customer 和 address 表映射 实体BEAN

 

两张实体都是OneToOne

customer

package ch03.com.ma.entity;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;

/**
* Customer generated by MyEclipse Persistence Tools
*/
@Entity
@Table(name = "customer", catalog = "test", uniqueConstraints = {})
public class Customer implements java.io.Serializable {

// Fields

/**
*
*/
private static final long serialVersionUID = 4432424287798285970L;

private Integer id;

private Address address;

private String name;

private String shortName;

private Double registeredCapital;

private String portrait;

// Constructors

/** default constructor */
public Customer() {
}

/** minimal constructor */
public Customer(Integer id) {
this.id = id;
}

/** full constructor */
public Customer(Integer id, Address address, String name, String shortName,
Double registeredCapital, String portrait) {
this.id = id;
this.address = address;
this.name = name;
this.shortName = shortName;
this.registeredCapital = registeredCapital;
this.portrait = portrait;
}

// Property accessors
@Id
@Column(name = "id", unique = true, nullable = false, insertable = true, updatable = true)
public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

@OneToOne(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
@JoinColumn(name = "addressid", unique = false, nullable = true, insertable = true, updatable = true)
public Address getAddress() {
return this.address;
}

public void setAddress(Address address) {
this.address = address;
}

@Column(name = "name", unique = false, nullable = true, insertable = true, updatable = true)
public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

@Column(name = "shortName", unique = false, nullable = true, insertable = true, updatable = true)
public String getShortName() {
return this.shortName;
}

public void setShortName(String shortName) {
this.shortName = shortName;
}

@Column(name = "registeredCapital", unique = false, nullable = true, insertable = true, updatable = true, precision = 10, scale = 0)
public Double getRegisteredCapital() {
return this.registeredCapital;
}

public void setRegisteredCapital(Double registeredCapital) {
this.registeredCapital = registeredCapital;
}

@Column(name = "portrait", unique = false, nullable = true, insertable = true, updatable = true)
public String getPortrait() {
return this.portrait;
}

public void setPortrait(String portrait) {
this.portrait = portrait;
}

}

 

address

package ch03.com.ma.entity;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

/**
* Address generated by MyEclipse Persistence Tools
*/
@Entity
@Table(name = "address", catalog = "test", uniqueConstraints = {})
public class Address implements java.io.Serializable {

// Fields

/**
*
*/
private static final long serialVersionUID = 3559085342537686006L;

private Integer id;

private String addressName;

private Customer customer ;

// Constructors

/** default constructor */
public Address() {
}

/** minimal constructor */
public Address(Integer id) {
this.id = id;
}

/** full constructor */
public Address(Integer id, String addressName, Customer customer) {
this.id = id;
this.addressName = addressName;
this.customer = customer;
}

// Property accessors
@Id
@Column(name = "id", unique = true, nullable = false, insertable = true, updatable = true)
public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

@Column(name = "addressName", unique = false, nullable = true, insertable = true, updatable = true)
public String getAddressName() {
return this.addressName;
}

public void setAddressName(String addressName) {
this.addressName = addressName;
}

@OneToOne(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "address")
public Customer getCustomer() {
return this.customer;
}

public void setCustomer(Customer customer) {
this.customer = customer;
}

}

 

 

customer的sessionBean

package ch03.com.ma.bo.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


import ch03.com.ma.bo.ICustomerService;
import ch03.com.ma.entity.Address;
import ch03.com.ma.entity.Customer;


@Stateless
public class CustomerService extends BaseService implements ICustomerService {

//@PersistenceContext(unitName = "hongmin.ma")
//private EntityManager entityManager;

public void delete(int id) {
Customer cusEO = this.findById(id);
entityManager.remove(cusEO);
}

@SuppressWarnings("unchecked")
public List<Customer> findAll(){
try{
String sql = " select c from Customer c";
Query query=entityManager.createQuery(sql);
List<Customer> list=query.getResultList();
return list;
}catch(Exception ex){
ex.printStackTrace();
return null;
}
}

public Customer findById(int id) {
Customer cusEO = entityManager.find(Customer.class, id);
return cusEO;
}

public void save(Customer cusEO,Address address) {
//entityManager.persist(address);
cusEO.setAddress(address);
entityManager.persist(cusEO);
}

public Customer update(Customer cusEO) {
Customer result = entityManager.merge(cusEO);
return result;
}

}

 

 

客户端

package action;

import java.util.Properties;

import javax.naming.InitialContext;


import ch03.com.ma.bo.ICustomerService;
import ch03.com.ma.common.EjbConnection;
import ch03.com.ma.entity.Address;
import ch03.com.ma.entity.Customer;


public class Test1 {
private static InitialContext ctx=null;

public static void main(String[] args) {


try {
ctx = EjbConnection.getConnection();

ICustomerService remote=(ICustomerService) ctx.lookup("CustomerService/remote");
// 客户
// 添加客户,同时添加地址
Address addEO=new Address();
addEO.setId(2);
addEO.setAddressName("江苏苏州");

Customer cusEO=new Customer();
cusEO.setId(2);
cusEO.setName("bbb");
cusEO.setShortName("b");
cusEO.setRegisteredCapital(10000d);

remote.save(cusEO,addEO);
System.out.println("添加成功");

} catch (Exception e) {
e.printStackTrace();
}


}


}

 

EJBConnection:

package ch03.com.ma.common;

import java.io.IOException;
import java.util.Properties;

import javax.naming.InitialContext;
import javax.naming.NamingException;

public class EjbConnection {
private static InitialContext ctx=null;
private static void init(){
Properties props=new Properties();
try {
props.load(EjbConnection.class.getResourceAsStream("../../../../config/EJB.properties"));
ctx=new InitialContext(props);
} catch (IOException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
}
}

public static synchronized InitialContext getConnection() {
if(ctx==null){
init();
}
return ctx;
}


public static void destroyConnection(){
if(ctx!=null){
try {
ctx.close();
ctx=null;
} catch (NamingException e) {
e.printStackTrace();
}

}
}
}