The one-to-many ing of EJB3 is set using @ onetoworkflow. If it is a bidirectional one-to-many ing, @ ManyToOne is required for the publisher. Two tables are provided in this book. The other table t_customers has been given in the previous article, and the structure 1 of the other table is shown. Figure 1 t_orders table
The t_mers MERs and t_orders tables have one-to-multiple relationships. One Customer may have multiple orders, while one Order can have only one Customer.
Define a set type attribute in the Customer class to save multiple Order objects. The code of the Customer class 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. manytoence;
Import javax. persistence. onetoence;
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;
Private collection <order> orders;
@ Onetoetype (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;
}
......
}
The mappedby attribute of @ onetoworkflow specifies the property name of the customer object obtained in the order class. The code for the Order class is as follows: 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 String productId;
Private int count;
Private Customer customer;
@ ManyToOne
@ JoinColumn (name = "customer_id ")
Public Customer getCustomer ()
{
Return customer;
}
Public void setCustomer (Customer customer)
{
This. customer = customer;
}
@ Id
@ GeneratedValue (strategy = GenerationType. IDENTITY)
Public int getId ()
{
Return id;
}
......
}
The name attribute of the @ JoinColumn comment specifies the foreign key name used to connect to the t_customers table in the t_orders table.
You can use the following code to test: Customer customer = new Customer ();
Customer. setName ("Microsoft ");
List <Order> orders = new ArrayList <Order> ();
Order order = 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 );
In addition, you can use @ jointable to specify the connection table to map the one-to-one relationship. The connection table structure 2 is shown in. Figure 2 t_customers_orders table
First, modify the getorders method of the customer class to the following format:
@ Onetoworkflow
@ 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;
}
The name attribute specifies the name of the connection table shown in figure 2. Joincolumns specifies the joined fields in the t_mers MERs table and the t_customers_orders table. Inversejoincolumns specifies the fields connected to the t_orders table and the t_customers_orders table. In this case, the customer_id field in the t_orders table is no longer needed. Therefore, remove the customer attribute (getter and setter methods) in the order table.
When the customer and order objects are persisted, the Order object must be persisted before the customer objects can be persisted.