Java Reference
In-Depth Information
in the
O/R
mapping and only uses
SQL
joins for those relationships specified by
fetch joins.
Let's look at how the code for the Place Order use case can use fetch joins.
Instead of loading the
PendingOrder
by calling
Session.load()
, the
PendingOrder-
Repository
methods called by
PlaceOrderService
method must load the
Pending-
Order
and the required related objects by executing a query that uses fetch joins.
When updating the payment information, the
PlaceOrderService
would call the
following method to load the pending order:
public class HibernatePendingOrderRepositoryImpl {
…
public PendingOrder
findPendingOrderWithRestaurantLineItemsAndMenuItems(
String pendingOrderId) {
return (PendingOrder) getHibernateTemplate()
.findByNamedQuery(
"PendingOrder.
bbbbbb
bb
➥
findPendingOrderWithRestaurantLineItemsAndMenuItems",
new Integer(pendingOrderId))
.get(0);
}
}
<hibernate-mapping>
…
<query name="PendingOrder.
bbbbbbb
b
➥
findPendingOrderWithRestaurantLineItemsAndMenuItems">
<![CDATA[
from PendingOrder po
left outer join fetch po.restaurant
left outer join fetch po.lineItems as lineItem
left outer join fetch lineItem.menuItem
where po.id = ?
]]></query>
…
</hibernate-mapping>
This code executes the named query, which uses fetch joins to eagerly load the
pending order's restaurant and its line item. Similarly, when updating the quanti-
ties the
PlaceOrderService
must call this method to load the pending order:
public class HibernatePendingOrderRepositoryImpl {
…
public PendingOrder findPendingOrderWithRestaurantAndMenuItems(
String pendingOrderId) {
return (PendingOrder) getHibernateTemplate()
.findByNamedQuery(
Search WWH ::
Custom Search