本文為原創(chuàng),如需轉(zhuǎn)載,請注明作者和出處,謝謝!
上一篇: eclipse + JBoss 5 + EJB3開發(fā)指南(8):實(shí)現(xiàn)Entity Bean的一對多(one-to-many)映射
在EJB3中需要使用@ManyToMany對封裝多對多關(guān)系的字段或getter方法進(jìn)行注釋。先看看下面的表:
下一篇: eclipse + JBoss 5 + EJB3開發(fā)指南(9):實(shí)現(xiàn)Entity Bean的多對多(many-to-many)映射
國內(nèi)最棒的Google Android技術(shù)社區(qū)(eoeandroid),歡迎訪問!
《銀河系列原創(chuàng)教程》 發(fā)布
《Java Web開發(fā)速學(xué)寶典》 出版,歡迎定購
上一篇: eclipse + JBoss 5 + EJB3開發(fā)指南(8):實(shí)現(xiàn)Entity Bean的一對多(one-to-many)映射
在EJB3中需要使用@ManyToMany對封裝多對多關(guān)系的字段或getter方法進(jìn)行注釋。先看看下面的表:

圖1 t_addresses表
t_addresses表和t_customers表是多對多的關(guān)系。需要使用一個(gè)關(guān)聯(lián)表來描述這種關(guān)系,關(guān)聯(lián)表的結(jié)構(gòu)如下圖所示。

圖2 t_customers_addresses
在Customer類中定義一個(gè)Collection<Address>類型的字段(addresses),用于保存與該Customer對象相對應(yīng)的多個(gè)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;
}
... ...
}
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表的連接字段關(guān)系。
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;
}
}
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;
}
}
由于是多對多的關(guān)系,因此,在Address類中需要定義一個(gè)Collection<Customer>類型的字段(customers)用來保存與該Address對象相對應(yīng)的Customer對象。getCustomers方法也需要使用@ManyToMany進(jìn)行注釋。可以使用下面代碼進(jìn)行測試:
<!--<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);
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開發(fā)指南(9):實(shí)現(xiàn)Entity Bean的多對多(many-to-many)映射
國內(nèi)最棒的Google Android技術(shù)社區(qū)(eoeandroid),歡迎訪問!
《銀河系列原創(chuàng)教程》 發(fā)布
《Java Web開發(fā)速學(xué)寶典》 出版,歡迎定購
eclipse + JBoss 5 + EJB3開發(fā)指南(9):實(shí)現(xiàn)Entity Bean的多對多(many-to-many)映射
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
