Java Reference
In-Depth Information
Defining the O/R mapping
Now that we have made that simple change to
PendingOrder
, the other thing we
must do is to write the
O/R
mapping document, which describes how its fields and
relationships map to the database schema. Let's first examine how the
Pending-
Order
class, its fields, and its relationships are mapped to the database schema;
after that we'll look at the Hibernate mapping document.
In chapter 4 we described how the
PendingOrder
class is mapped to the
PENDING_ORDER
table. The
id
field is mapped to the
PENDING_ORDER_ID
column,
which is the table's primary key. Its simple fields are mapped to columns of this
table. The
state
field is mapped to the
STATE
column, and the
deliveryTime
field
is mapped to the
DELIVERY_TIME
column.
The
deliveryAddress
and
paymentInformation
fields reference embedded
value objects, and so the fields of the
Address
and
PaymentInformation
objects are
mapped to the columns of the
PENDING_ORDER
table. For example, the
street1
field of the
Address
object is mapped to the
DELIVERY_STREET1
column of the
PENDING_ORDER
table.
The
PendingOrder
-
PendingOrderLineItem
relationship is an ordered, unidirec-
tional, one-to-many relationship. In addition, a
PendingOrderLineItem
must be
deleted when its
PendingOrder
is deleted or when it is no longer associated with a
PendingOrder
. We saw in section 6.1.1 that Hibernate provides two ways to map this
kind of relationship: as an entity collection or as a component collection. Because
the line items are only referenced by
PendingOrder
, we can use a component col-
lection, which is slightly more efficient because Hibernate uses fewer
SQL
state-
ments.
PendingOrderLineItems
are mapped to the
PENDING_ORDER_LINE_ITEM
table, which has a foreign key to the
PENDING_ORDER
table, and a
LINE_ITEM_INDEX
column that stores the position of the line item.
The
PendingOrder
-
Restaurant
and
PendingOrder
-
Coupon
relationships are uni-
directional, many-to-one relationships. The
PENDING_ORDER
table has a
RESTAURANT_ID
column, which is a foreign key to the
RESTAURANT
table, and a
COUPON_ID
column, which is a foreign key to the
COUPON
table.
Listing 6.3 shows PendingOrder.hbm.xml, which is the
O/R
mapping document
for
PendingOrder
. PendingOrder.hbm.xml must be accessible at runtime and is
often located in the same directory of the class and on the class path. In chapter 7,
we will look at how to create a
SessionFactory
that uses PendingOrder.hbm.xml.
Search WWH ::
Custom Search