Eclipse+JBoss+EJB3实现Entity Bean的多对多映射

开发 后端
本文介绍如何在Eclipse+JBoss+EJB3下实现Entity Bean的多对多映射。在EJB3中需要使用@ManyToMany对封装多对多关系的字段或getter方法进行注释。

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

由于是多对多的关系,因此,在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. Eclipse+JBoss+EJB3配置文件发布Session Bean
  2. Eclipse+JBoss+EJB3编写有状态的SessionBean
  3. Eclipse+JBoss+EJB3实现Entity Bean的一对多映射
  4. Eclipse+JBoss+EJB3实现Entity Bean的一对一映射
  5. Eclipse+JBoss+EJB3编写第一个实体Bean程序
责任编辑:book05 来源: BlogJava
相关推荐

2009-06-24 15:51:47

Entity Bean一对多映射

2009-06-24 15:49:54

Entity Bean一对一映射

2009-06-24 15:59:04

消息驱动Bean

2009-06-10 11:42:26

Session BeaEclipse+JBo

2009-06-24 15:56:47

实体Bean连接策略

2009-06-10 12:34:01

Session BeaEclipse+JBo

2009-06-10 11:09:40

配置文件SessionEclipse+JBo

2009-06-10 11:36:45

有状态的SessionEclipse+JBo

2009-06-24 15:47:13

实体Bean

2009-06-24 15:55:09

EclipseJBossJ2ee

2009-06-24 15:57:44

JPQL命名查询

2009-06-24 15:58:15

EntityManag

2009-06-24 16:00:00

2009-06-10 12:54:35

无状态的SessionEclipse+JBo

2009-09-24 12:05:35

2009-07-21 17:39:50

iBATIS的多对多映

2009-06-18 14:22:06

Hibernate多对Hibernate

2009-07-21 17:31:39

iBATIS一对多映射

2009-06-04 16:14:22

Hibernate一对Hibernate一对Hibernate多对

2009-06-15 16:06:25

JBoss IDE
点赞
收藏

51CTO技术栈公众号