Java Reference
In-Depth Information
You can, for example, configure the
PendingOrder
-
Restaurant
and
PendingOrder-
LineItem
-
MenuItem
relationships to be eagerly loaded with an order as follows:
<hibernate-mapping>
<class name="PendingOrder"
table="PLACED_ORDER">
…
<many-to-one name="restaurant"
fetch="join"
column="RESTAURANT_ID"
…
/>
</class>
<class name="PendingOrderLineItem"
table="PLACED_ORDER">
…
<many-to-one name="MenuItem"
fetch="join"
column="RESTAURANT_ID"
/>
…
</class>
</hibernate-mapping>
The
fetch="join"
attribute of the
mapping
element for the
PendingOrder
-
Restau-
rant
relationship tells Hibernate to load a
PendingOrder
by executing a
SQL
SELECT
statement that does an outer join between the
PENDING_ORDER
and
RES-
TAURANT
tables to retrieve both the pending order and its restaurant. The
fetch="join"
attribute for the
PendingOrderLineItem
-
MenuItem
has a similar
effect. With this configuration, calling
Session.load()
with the
PendingOrder
class and a pending order
id
will load a pending order, its restaurant, its line
items, and their menu items using two
SELECT
statements. The first loads the
pending order and the restaurant, and the second loads the line items and their
menu items.
Although using the
fetch
attribute to configure eager loading for a relation-
ship might appear to be straightforward, there are a couple of important things to
remember. First, the
fetch
attribute only affects how objects are loaded by:
get()
and
load()
■
Navigation from one object to another
■
Criteria queries, which are described in detail in chapter 11
■
Search WWH ::
Custom Search