Java Reference
In-Depth Information
objects eagerly, which can make the code more complicated. Instead of defining a
single
findPendingOrder()
method, the
PendingOrderRepository
must define mul-
tiple methods for retrieving
PendingOrder
s, such as
findPendingOrderWith-
RestaurantAndLineItems()
and
findPendingOrderWithRestaurantAndMenuItems()
.
This complicates the design of the domain model and makes it more difficult to
design a reusable domain model because you must anticipate how it will be used.
In comparison,
JDO
configures eager loading using fetch groups, which are
defined declaratively and are separate from the code. They also have the added
benefit of controlling eager loading during navigation, which is a feature that
Hibernate lacks.
Now that you have seen how to optimize database accesses by using eager load-
ing, let's look at how to reduce database accesses by using a process-level cache.
6.5.2
Using a process-level cache
By default, Hibernate caches objects in
Session
, which typically means that
objects are cached for the duration of the request. A Hibernate application can
also use a process-level cache that caches objects across sessions and hence
requests. Before accessing the database to load an object, Hibernate will first look
in the
Session
cache and then in the process-level cache. A process-level cache
can significantly reduce database accesses if the application accesses the same
date repeatedly. The process-level cache is best used to store objects that change
relatively infrequently.
Hibernate has a pluggable caching architecture that supports a variety of dif-
ferent caching frameworks, which have varying capabilities. For example, Hiber-
nate ships with Ehcache [EHCache], which is a simple and efficient cache for use
in nonclustered environments. Examples of caching frameworks that work in a
clustered environment are SwarmCache [SwarmCache] and JBoss Cache [JBoss-
Cache]. For more information on how to configure these classes, please consult
the Hibernate documentation.
In Hibernate, caching is configured on a per-class and per-collection basis.
Hibernate supports a variety of caching strategies including:
read-only—
For read-only objects that are never modified by the application
■
read/write
—For objects that are modified by the application
■
In this example, the restaurant-related classes—
Restaurant
,
MenuItem
, and
Time-
Range
—are rarely updated and thus are good candidates for process-level caching.
To cache these classes in the process-level cache, we must use the
<cache>
element
Search WWH ::
Custom Search