Java Reference
In-Depth Information
In this section you will learn how to use eager loading, process-level caching,
and query caching in a
JDO
application. We describe how to use
JDO
fetch groups
to configure eager loading. You will learn how to use
AOP
to separate the code
that configures the eager loading from the code the core business logic. We dis-
cuss how to use process-level caching and query caching in one popular
JDO
implementation, and use the Place Order use case as an example.
5.4.1
Using fetch groups to optimize object loading
JDO
, like most other
ORM
frameworks, uses lazy loading by default. You configure
eager loading by using either
JDO
fetch groups or an implementation-specific
mechanism. A
JDO
fetch group, which is associated with a class, describes the
structure of an object graph whose root object is an instance of that class. It speci-
fies the objects and their fields that should be loaded when the application exe-
cutes a
JDO
query or loads an instance. There are two ways to eagerly load a
related object using fetch groups. The simpler of the approaches is to add the
field that references the object to what is called the
default fetch group
. The other
approach is to define a
custom fetch group
. Let's look at each strategy.
Using default fetch groups
Every persistent
JDO
class has a default fetch group, which contains the fields that
the
JDO
implementation loads by default. By default, this group contains the class's
primitive fields, the date fields, string fields, and fields whose type is a number
wrapper class. As a result, the
JDO
implementation will only load those fields that
contain simple values and any referenced objects will be loaded lazily. However, an
easy way to eagerly load an object is to add the field that references it to the default
fetch group.
For example, the application always traverses the
PendingOrder
-
Restaurant
and
PendingOrderLineItem
-
MenuItem
relationships in the Food to Go Domain
model. It makes sense, therefore, to add the corresponding fields to the default
fetch group. The default fetch group is configured in the
XML
metadata. For
example, this is how you would add the
PendingOrder.restaurant
field to the
default fetch group:
<class name="PendingOrder" identity-type="application">
…
<field name="restaurant" default-fetch-group="true">
…
</class>
Search WWH ::
Custom Search