在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類型的字段(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);