程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2EE >> Eclipse+JBoss+EJB3實現Entity Bean的多對多映射

Eclipse+JBoss+EJB3實現Entity Bean的多對多映射

編輯:J2EE

在EJB3中需要使用@ManyToMany對封裝多對多關系的字段或getter方法進行注釋。先看看下面的表:

t_addresses表 圖1 t_addresses表

t_addresses表和t_customers表是多對多的關系。需要使用一個關聯表來描述這種關系,關聯表的結構如下圖所示。

t_customers_addresses 圖2 t_customers_addresses

在Customer類中定義一個Collection < ADDRESS>類型的字段(addresses),用於保存與該Customer對象相對應的多個Address對象,代碼如下:

  1. package entity;
  2. import Java.util.Collection;
  3. import Javax.persistence.CascadeType;
  4. import Javax.persistence.Entity;
  5. import Javax.persistence.FetchType;
  6. import Javax.persistence.GeneratedValue;
  7. import Javax.persistence.GenerationType;
  8. import Javax.persistence.Id;
  9. import Javax.persistence.JoinColumn;
  10. import Javax.persistence.JoinTable;
  11. import Javax.persistence.ManyToMany;
  12. import Javax.persistence.OneToMany;
  13. import Javax.persistence.OneToOne;
  14. import Javax.persistence.PrimaryKeyJoinColumn;
  15. import Javax.persistence.Table;
  16. @Entity
  17. @Table(name = "t_customers")
  18. public class Customer
  19. {
  20. private int id;
  21. private String name;
  22. private Referee referee;
  23. private Collection< Order> orders;
  24. private Collection< Address> addresses;
  25. @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
  26. @JoinTable(name = "t_customers_addresses", joinColumns = @JoinColumn(name = "customer_id",
  27. referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "address_id", referencedColumnName = "id"))
  28. public Collection< Address> getAddresses()
  29. {
  30. return addresses;
  31. }
  32. ... ...
  33. }
  34. @JoinTable注釋用於指定連接表和t_customers及t_addresses表的連接字段關系。
  35. Address類的代碼如下:
  36. package entity;
  37. import Java.util.Collection;
  38. import Javax.persistence.CascadeType;
  39. import Javax.persistence.Entity;
  40. import Javax.persistence.FetchType;
  41. import Javax.persistence.GeneratedValue;
  42. import Javax.persistence.GenerationType;
  43. import Javax.persistence.Id;
  44. import Javax.persistence.ManyToMany;
  45. import Javax.persistence.Table;
  46. @Entity
  47. @Table(name="t_addresses")
  48. public class Address
  49. {
  50. private int id;
  51. private String addressLine;
  52. private String country;
  53. private String postCode;
  54. private Collection< Customer> customers;
  55. @Id
  56. @GeneratedValue(strategy=GenerationType.IDENTITY)
  57. public int getId()
  58. {
  59. return id;
  60. }
  61. public void setId(int id)
  62. {
  63. this.id = id;
  64. }
  65. public String getAddressLine()
  66. {
  67. return addressLine;
  68. }
  69. public void setAddressLine(String addressLine)
  70. {
  71. this.addressLine = addressLine;
  72. }
  73. public String getCountry()
  74. {
  75. return country;
  76. }
  77. public void setCountry(String country)
  78. {
  79. this.country = country;
  80. }
  81. public String getPostCode()
  82. {
  83. return postCode;
  84. }
  85. public void setPostCode(String postCode)
  86. {
  87. this.postCode = postCode;
  88. }
  89. @ManyToMany(mappedBy="addresses")
  90. public Collection< Customer> getCustomers()
  91. {
  92. return customers;
  93. }
  94. public void setCustomers(Collection< Customer> customers)
  95. {
  96. this.customers = customers;
  97. }
  98. }

由於是多對多的關系,因此,在Address類中需要定義一個Collection類型的字段(customers)用來保存與該Address對象相對應的Customer對象。getCustomers方法也需要使用@ManyToMany進行注釋。可以使用下面代碼進行測試:

  1. Customer customer = new Customer();
  2. customer.setName("微軟11");
  3. List< Address> addresses = new ArrayList< Address>();
  4. Address address = new entity.Address();
  5. address.setAddressLine("address1");
  6. address.setCountry("中國");
  7. address.setPostCode("12345678");
  8. addresses.add(address);
  9. address = new entity.Address();
  10. address.setAddressLine("address2");
  11. address.setCountry("美國");
  12. address.setPostCode("4321");
  13. addresses.add(address);
  14. customer.setAddresses(addresses);
  15. em.persist(customer);

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved