實現Entity Bean的多對多(many-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);