Java Reference
In-Depth Information
"PendingOrder.
bbbbbb bb findPendingOrderWithRestaurantAndMenuItems",
new Integer(pendingOrderId))
.get(0);
}
}
<hibernate-mapping>
<query name="PendingOrder.
bbbbbbb bb findPendingOrderWithRestaurantAndMenuItems">
<![CDATA[
from PendingOrder po
left outer join fetch po.restaurant as r
left outer join fetch r.menuItems
where po.id = ?
]]></query>
</hibernate-mapping>
This code executes the named query, which eagerly loads the pending order's res-
taurant and its menu items. In each case, Hibernate loads all the required objects
using a single SQL SELECT statement that does a multiway join between the
required tables.
Things to remember when using fetch joins
Fetch joins are a simple and concise way to eagerly load objects dynamically. But
there are several things you need to remember when using them. First, if you
retrieve a collection using a fetch join, then the ResultSet returned by the SELECT
statement might contain duplicate data. In the previous example, which retrieves
the line items, columns from the PLACED_ORDER and RESTAURANT tables are
duplicated in every row. This can impact performance if the query returns a large
number of rows and columns.
Second, a query can only use a fetch join on a single collection. The other col-
lections will have to be loaded using separate queries or lazily. This prevents per-
formance problems caused by Hibernate executing a SQL SELECT statement that
returns the Cartesian product of two or more large collections.
Finally, it is important to use a fetch join for all references to nonlazy objects
and collections in order to prevent performance problems caused by Hibernate
loading those objects using additional SQL SELECT statements. This is a variation
of the N +1 query problem because if a query returns N objects, Hibernate will exe-
cute N additional queries to load the related objects.
A significant limitation of Hibernate's fetch join mechanism is that the applica-
tion must have multiple versions of a query if different requests load different
Search WWH ::




Custom Search