Java Reference
In-Depth Information
or the line items will not be loaded until the application navigates to them. As a
result, the application executes several
SQL
SELECT
statements:
select … from PENDING_ORDER po
where po.PENDING_ORDER_ID=?
select … from RESTAURANT r
where r.RESTAURANT_ID=?
select … from PENDING_ORDER_LINE_ITEM li
where li.PENDING_ORDER_ID=?
select … from MENU_ITEM mi
where mi.MENU_ITEM_ID=?
select … from MENU_ITEM mi
where mi.MENU_ITEM_ID=?
…
The first
SQL
statement is executed when
PlaceOrderService
loads
PendingOrder
.
The next two
SQL
statements are executed when the application navigates from
PendingOrder
to its restaurant and line items. The remaining
SQL
statements are
executed as the application navigates from each line to its menu item. This might
not seem like a lot of
SQL
statements, but imagine if the application is handling
tens or hundreds of requests per second. Let's look at how eager loading can used
to improve performance.
4.6.2
Configuring eager loading
There are two ways to use configure eager loading for a relationship. One option
is to
statically
configure eager loading, which means that a related object will
always be loaded with its referencing object. The other option is to
dynamically
configure eager loading and only load related objects when handling some
requests. In order to know which relationships need to be eagerly loaded and to
determine whether to do it statically or dynamically, we must identify the relation-
ships that are traversed while handling each kind of request.
There are a few methods of determining which relationships are traversed
when handling each request. One is to look at the code and see what relationships
it traverses. Another is to run the application with logging enabled and examine
the
SQL
statements that are executed. Alternatively, the persistence framework
might have a tool that provides this information. Once you have done this analy-
sis, you will know which relationships are always traversed and which relationships
are only traversed by some requests.
As you can see in figure 4.4, if we analyze the Place Order use we will find that
the application always traverses the
PendingOrder
-
Restaurant
and
PendingOrder-
Line
-
MenuItem
relationships and only sometimes traverses other relationships such
as
Restaurant
-
MenuItem
and
PendingOrder
-
PendingOrderLineItem
.
Search WWH ::
Custom Search