eclipse + JBoss 5 + EJB3开发指南(9):实现E

系统 2000 0
本文为原创,如需转载,请注明作者和出处,谢谢!

上一篇: 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对象,代码如下:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> 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 Stringname;
private Refereereferee;
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类的代码如下:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> 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 StringaddressLine;
private Stringcountry;
private StringpostCode;
private Collection < Customer > customers;
@Id
@GeneratedValue(strategy
= GenerationType.IDENTITY)
public int getId()
{
return id;
}
public void setId( int id)
{
this .id = id;
}
public StringgetAddressLine()
{
return addressLine;
}
public void setAddressLine(StringaddressLine)
{
this .addressLine = addressLine;
}
public StringgetCountry()
{
return country;
}
public void setCountry(Stringcountry)
{
this .country = country;
}
public StringgetPostCode()
{
return postCode;
}
public void setPostCode(StringpostCode)
{
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进行注释。可以使用下面代码进行测试:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> Customercustomer = new Customer();
customer.setName(
" 微软11 " );
List < Address > addresses = new ArrayList < Address > ();
Addressaddress
= 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)映射

国内最棒的Google Android技术社区(eoeandroid),欢迎访问!

《银河系列原创教程》 发布

《Java Web开发速学宝典》 出版,欢迎定购

eclipse + JBoss 5 + EJB3开发指南(9):实现Entity Bean的多对多(many-to-many)映射


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论