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

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

上一篇: eclipse + JBoss 5 + EJB3开发指南(7):实现Entity Bean的一对一(one-to-one)映射

EJB3的一对多映射使用@OneToMany来设置,如果是双向的一对多映射,在many方需要使用@ManyToOne设置。在本书中给出两个表,其他一个表t_customers在上一篇文章中已给出了,另一个表的结构如图1所示。
图1 t_orders表

t_customers和t_orders表是一对多关系,一个Customer可能有多个Order,而一个Order只能有一个Customer。
在Customer类中需要定义一个集合类型的属性,用来保存多个Order对象,Customer类的代码如下:
<!--<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;

@OneToMany(mappedBy = " customer " ,cascade = CascadeType.ALL)
public Collection < Order > getOrders()
{
return orders;
}
public void setOrders(Collection < Order > orders)
{
this .orders = orders;
}
@Id
@GeneratedValue(strategy
= GenerationType.IDENTITY)
public int getId()
{
return id;
}
... ...
}
其中@OneToMany的mappedBy属性指定了Order类中获得Customer对象的属性名。Order类的代码如下:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name
= " t_orders " )
public class Order
{
private int id;
private StringproductId;
private int count;
private Customercustomer;

@ManyToOne
@JoinColumn(name
= " customer_id " )
public CustomergetCustomer()
{
return customer;
}

public void setCustomer(Customercustomer)
{
this .customer = customer;
}

@Id
@GeneratedValue(strategy
= GenerationType.IDENTITY)
public int getId()
{
return id;
}
... ...


}
其中@JoinColumn注释的name属性指定t_orders表中用于连接t_customers表的外键名。
可以使用下面的代码进行测试:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> Customercustomer = new Customer();
customer.setName(
" 微软 " );
List < Order > orders = new ArrayList < Order > ();
Orderorder
= new Order();
order.setProductId(
" 1234 " );
order.setCount(
20 );
order.setCustomer(customer);
orders.add(order);
order
= new Order();
order.setProductId(
" 4321 " );
order.setCount(
12 );
order.setCustomer(customer);
orders.add(order);
customer.setOrders(orders);
em.persist(customer);

除此之外,还可以使用@JoinTable指定连接表来映射one-to-many关系。连接表的结构如图2所示。
图2 t_customers_orders表

首选需要将Customer类的getOrders方法修改成下的形式:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> @OneToMany
@JoinTable(name
= " t_customers_orders " ,joinColumns = @JoinColumn(name = " t_customers_id " ,
referencedColumnName
= " id " ),inverseJoinColumns =
@JoinColumn(name
= " orders_id " ,referencedColumnName = " id " ))
public Collection < Order > getOrders()
{
return orders;
}

其中name属性指定图2所示的连接表的名称。joinColumns指定了t_customers表和t_customers_orders表中相连接的字段。inverseJoinColumns指定了t_orders表和t_customers_orders表中相连接的字段。这时t_orders表中的customer_id字段就不再需要了。因此,需要将Order表中的customer属性(getter和setter方法)去掉。

在持久化Customer和Order对象时,需要对Order对象进行持久化后才能对Customer对象进行持久化。

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



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

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

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

你可能感兴趣的:(eclipse,android,bean,jboss,出版)