Entity Relationships in the order Application

The order application demonstrates several types
of entity relationships: one-to-many, many-to-one, one-to-one, unidirectional,
and self-referential relationships.

Self-Referential Relationships

A self-referential relationship is a relationship
between relationship fields in the same entity. Part has
a field bomPart that has a one-to-many relationship with
the field parts, which is also in Part.
That is, a part can be made up of many parts, and each of those parts has
exactly one bill-of-material part.

The primary key for Part is a compound primary
key, a combination of the partNumber and revision fields.
It is mapped to the PARTNUMBER and REVISION columns
in the EJB_ORDER_PART table.

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.

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.