background image

One-to-Many Relationship

<< Self-Referential Relationships | Generated Primary Keys >>
<< Self-Referential Relationships | Generated Primary Keys >>

One-to-Many Relationship

referencedColumnName=
"REVISION")
})
public Part getPart() {
return part;
}
Note that, because Part uses a compound primary key, the @JoinColumns annotation is used to
map the columns in the EJB_ORDER_VENDOR_PART table to the columns in EJB_ORDER_PART.
EJB_ORDER_VENDOR_PART
's PARTREVISION column refers to EJB_ORDER_PART's REVISION
column.
One-to-Many Relationship Mapped to Overlapping Primary and
Foreign Keys
Order
has a field, lineItems, that has a one-to-many relationship with LineItem's field order.
That is, each order has one or more line item.
LineItem
uses a compound primary key that is made up of the orderId and itemId fields. This
compound primary key maps to the ORDERID and ITEMID columns in the EJB_ORDER_LINEITEM
database table. ORDERID is a foreign key to the ORDERID column in the EJB_ORDER_ORDER table.
This means that the ORDERID column is mapped twice: once as a primary key field, orderId; and
again as a relationship field, order.
Here's the relationship mapping in Order:
@OneToMany(cascade=ALL, mappedBy=
"order")
public Collection<LineItem> getLineItems() {
return lineItems;
}
Here is the relationship mapping in LineItem:
@ManyToOne
public Order getOrder() {
return order;
}
Unidirectional Relationships
LineItem
has a field, vendorPart, that has a unidirectional many-to-one relationship with
VendorPart
. That is, there is no field in the target entity in this relationship.
@ManyToOne
public VendorPart getVendorPart() {
return vendorPart;
}
The order Application
Chapter 26 · Persistence in the EJB Tier
713