Implement Many-to-many (Many-to-many) mappings for entity beans
You need to use @manytomany to annotate fields or getter methods that encapsulate many-to-many relationships in EJB3. First look at the following table:
Figure 1 T_addresses Table
The T_addresses table and the T_customers table are many-to-many relationships. You need to use an association table to describe this relationship, and the structure of the associated table is shown in the following illustration.
Figure 2 T_customers_addresses
Define a Collection<address> Type field (addresses) in the customer class to hold multiple address objects corresponding to the Customer object, 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;
private collection<address> addresses; @ManyToMany (cascade = cascadetype.persist, fetch = Fetchtype.lazy) @JoinTable (name = "T_customers_addresses", Joincol Umns = @JoinColumn (name = "Customer_ID", referencedcolumnname = "id"), inversejoincolumns = @JoinColumn (name = "address_id", referencedcolumnname = "id")) public collection<address> getaddresses ()
{return addresses; } ... ... }