Java Reference
In-Depth Information
no distinct identity and stored in their parent object's table. The lifetime of each
PendingOrderLineItem
,
OrderLineItem
, and
MenuItem
object is dependent on its
parent.
PendingOrderLineItem
and
OrderLineItem
could be either entities or com-
ponents, but
MenuItem
needs to be an entity because it is referenced by
Pending-
OrderLineItem
,
OrderLineItem
, and
Restaurant
.
Later in this section, we'll describe these choices in more detail when we look at
how to define the
O/R
mapping for the various relationships in the domain model.
6.1.3
Configuring object identity
A persistent object has a persistent identity, which is the primary key of the corre-
sponding row the database table. The persistent identity usually consists of a sin-
gle value, which maps to a primary key column. A class can also have a composite
key consisting of multiple properties that map to multiple table columns, but we
don't describe this feature here. In the Hibernate
O/R
mapping for a class, you
can specify various aspects of its persistent identity, including the primary key col-
umn, whether it is maintained in the object, and whether the application or
Hibernate generates the persistent identity. Let's look at an example.
An example of how to configure the identity
A class's persistent identity is configured using the
<id>
and
<generator>
elements
in the mapping document:
<class name="PendingOrder" table="PENDING_ORDER">
<id name="id" column="PENDING_ORDER_ID">
<generator class="native"/>
</id>
…
</class>
In this example the
<id>
and
<generator>
elements specify the following informa-
tion about the persistent identity of the
PendingOrder
class:
The
PENDING_ORDER_ID
column is the primary key for the
PENDING_ORDER
table.
■
The
PendingOrder.id
field stores the persistent identity.
■
Hibernate should generate persistent identifiers using a database-specific
mechanism such as an Oracle sequence or an identity column.
■
Now that you have seen an example, let's look at the decisions you must make
when configuring Hibernate object identity.
Search WWH ::
Custom Search