EJB 3 persistence supports bi-directional one-to-many association mapping, unidirectional one-to-one association mapping and unidirectional one-to-many association mapping.
A OneToMany annotation is generated to define a many-valued association with one-to-many multiplicity. A ManyToOne annotation is generated to define a single-valued association to another entity class that has many-to-one multiplicity. The JoinColumn annotation is generated to specify a join column for the reference associating the tables. For bi-directional one-to-many associations, generated annotations will resemble:
@OneToMany(fetch=FetchType.EAGER, mappedBy="customer") public java.util.Collection<Order> getOrder() { ... }
@ManyToOne @JoinColumns({ @JoinColumn(name="cid", referencedColumnName="cid") }) public Customer getCustomer() { ... }
Generated annotations for unidirectional many-to-one associations are similar. A model check is available to verify that mappings for bi-directional one-to-many associations and unidirectional many-to-one associations are correctly defined. The references can only navigate from primary tables of classes on the multiple-valued side to primary tables of classes on the single-valued side.
For unidirectional one-to-many association, the JoinTable annotation is generated to define middle table and join columns for the two reference keys.
@OneToMany(fetch=FetchType.EAGER) @JoinTable( name="Customer_Order", joinColumns={ @JoinColumn(name="cid", referencedColumnName="cid") }, inverseJoinColumns={ @JoinColumn(name="oid", referencedColumnName="orderId") } ) public java.util.Collection<Order> getOrder() { ... }
A model check is available to verify that mappings for unidirectional one-to-many associations are correctly defined. Middle tables are needed for this kind of one-to-many association mapping.
One-to-many associations where the primary key is migrated are not supported in EJB 3.
For more informations about mapping, see O/R Mapping Modeling.