本文为原创,如需转载,请注明作者和出处,谢谢!
上一篇:
eclipse + JBoss 5 + EJB3开发指南(8):实现Entity Bean的一对多(one-to-many)映射
在EJB3中需要使用@ManyToMany对封装多对多关系的字段或getter方法进行注释。先看看下面的表:
图1 t_addresses表
t_addresses表和t_customers表是多对多的关系。需要使用一个关联表来描述这种关系,关联表的结构如下图所示。
图2 t_customers_addresses
在Customer类中定义一个Collection<Address>类型的字段(addresses),用于保存与该Customer对象相对应的多个Address对象,代码如下:
package entity;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
@Entity
@Table(name = "t_customers")
public class Customer
{
private int id;
private String name;
private Referee referee;
private Collection<Order> orders;
private Collection<Address> addresses;
@ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
@JoinTable(name = "t_customers_addresses", joinColumns = @JoinColumn(name = "customer_id",
referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "address_id", referencedColumnName = "id"))
public Collection<Address> getAddresses()
{
return addresses;
}
... ...
}
@JoinTable注释用于指定连接表和t_customers及t_addresses表的连接字段关系。
Address类的代码如下:
package entity;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="t_addresses")
public class Address
{
private int id;
private String addressLine;
private String country;
private String postCode;
private Collection<Customer> customers;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getAddressLine()
{
return addressLine;
}
public void setAddressLine(String addressLine)
{
this.addressLine = addressLine;
}
public String getCountry()
{
return country;
}
public void setCountry(String country)
{
this.country = country;
}
public String getPostCode()
{
return postCode;
}
public void setPostCode(String postCode)
{
this.postCode = postCode;
}
@ManyToMany(mappedBy="addresses")
public Collection<Customer> getCustomers()
{
return customers;
}
public void setCustomers(Collection<Customer> customers)
{
this.customers = customers;
}
}
由于是多对多的关系,因此,在Address类中需要定义一个Collection<Customer>类型的字段(customers)用来保存与该Address对象相对应的Customer对象。getCustomers方法也需要使用@ManyToMany进行注释。可以使用下面代码进行测试:
Customer customer = new Customer();
customer.setName("微软11");
List<Address> addresses = new ArrayList<Address>();
Address address = new entity.Address();
address.setAddressLine("address1");
address.setCountry("中国");
address.setPostCode("12345678");
addresses.add(address);
address = new entity.Address();
address.setAddressLine("address2");
address.setCountry("美国");
address.setPostCode("4321");
addresses.add(address);
customer.setAddresses(addresses);
em.persist(customer);
下一篇:
eclipse + JBoss 5 + EJB3开发指南(9):实现Entity Bean的多对多(many-to-many)映射
新浪微博:http://t.sina.com.cn/androidguy 昵称:李宁_Lining