Java Reference
In-Depth Information
cascade="none" />
</composite-element>
</list>
…
</class>
This mapping specifies that the
lineItems
property is a collection of
Pending-
OrderLineItem
components that is stored in the
PENDING_ORDER_LINE_ITEM
table. Notice that because
PendingOrderLineItem
is mapped as a component it
does not have a separate
<class>
mapping.
Here is the definition of the
PENDING_ORDER_LINE_ITEM
table:
CREATE TABLE PENDING_ORDER_LINE_ITEM (
QUANTITY NUMBER(10) NOT NULL,
PENDING_ORDER_ID NUMBER(10) NOT NULL,
LINE_ITEM_INDEX NUMBER(10) NOT NULL,
MENU_ITEM_ID NUMBER(10),
)
The
PENDING_ORDER_ID
column is a foreign key to the
PENDING_ORDER
table, and
the
MENU__ITEM_ID
column is a foreign key to the
MENU_ITEM
table. Notice that
this table does not have a surrogate primary key. Instead, the primary key consists
of the
PENDING_ORDER_LINE_ITEM_ID
and
LINE_ITEM_INDEX
columns.
A benefit of using component collections is that Hibernate persists a newly cre-
ated child by executing a single
INSERT
SQL
statement. There are, however, a cou-
ple of limitations. The child table cannot use a surrogate key, which means, for
example, that Hibernate will not set a surrogate primary key column to a gener-
ated value. If the table must have a surrogate key—in order to comply with data-
base schema design guidelines, for instance—the application must use a trigger to
initialize the primary key.
Another important limitation of component collections is that the child class
can only be referenced by its parent. Because of this restriction, we cannot use a
component collection for the
Restaurant
-
MenuItem
relationship since menu items
are also referenced by pending order line items and order line items. We can,
however, use component collections for the
PendingOrder
-
PendingOrderLineItem
and
Order
-
OrderLineItem
relationships because line items are referenced only by
their parent.
6.1.4
Using the cascade attribute
In chapter 4 we saw that an
ORM
framework must participate in the creation and
destruction of a persistent object in order to update the database. For example,
Search WWH ::
Custom Search