Implement One-to-many (One-to-many) mappings for entity beans
EJB3 's One-to-many mappings are set using @onetomany, and if it is a two-way one-to-many mapping, the @manytoone setting is required on the many side. Given two tables in this book, the other table t_customers in the previous article, and the structure of the other table is shown in Figure 1.
Figure 1 T_orders Table
T_customers and T_orders tables are a one-to-many relationship, a customer may have multiple order, and an order can only have one customer.
You need to define a property of a collection type in the customer class to hold multiple order objects, and the customer class code is as follows:
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 String name;
Private referee Referee;
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; } ... ... }