Java Reference
In-Depth Information
get an object's identity by calling Session.getIdentity() with the object as a
parameter, which is much less convenient.
The downside of using identifier properties is that because they are usually sur-
rogate keys that are not part of the domain model. For example, none of the
classes in the Food to Go domain model developed in chapter 3 has an identifier
property. Consequently, if you want to use identifier properties you must add
them to the domain model classes. However, this is usually worthwhile for those
objects whose identity is accessed by the application.
Mapping one-to-many relationships
An important part of defining the O/R mapping for a domain model is mapping
relationships to the database schema. Hibernate's rich ORM features makes this
mostly straightforward. However, one tricky area is defining the mapping for uni-
directional one-to-many relationships such as PendingOrder - PendingOrderLineItem
and Restaurant - MenuItem . In the domain model, these relationships are imple-
mented by a collection of child objects in the parent object, and in the database
they are represented by a foreign key from the child table to the parent table.
Because of the way Hibernate handles this kind of relationship, you must some-
times choose between using suboptimal SQL or changing the domain model and
database schema. Hibernate provides two ways to define the mapping for this kind
of relationship. Let's look at the details of each approach and its respective benefits
and drawbacks.
Using entity collections
One approach is to map the one-to-many relationship as a collection of Hibernate
entities. For example, we saw in section 6.1.1 that the MenuItem class must be mapped
as a Hibernate entity because it is referenced by multiple classes. This means that
we must map the Restaurant - MenuItems relationship as an entity collection:
<class name="Restaurant" table="RESTAURANT">
<list name="menuItems"
bbbbbbb b cascade="all,delete-orphan">
bbbbb <key column="RESTAURANT_ID" not-null="true"/>
bbbbb <index column="MENU_ITEM_INDEX"/>
bbbbb <one-to-many class="MenuItem">
</list>
</class>
<class name="MenuItem" table="MENU_ITEM">
<property name="name" column="NAME"/>
 
 
Search WWH ::




Custom Search