Java Reference
In-Depth Information
relationship, you might want to configure it to be always eagerly loaded. Con-
versely, you might want to dynamically configure eager loading for relationships
that are only traversed by the application when handling particular requests.
Whereas eager loading improves performance by loading related objects with
a single SELECT statement, process-level caching improves performance by elimi-
nating some SELECT statements. Instead of retrieving objects from the database,
the application retrieves them from the process-level cache. Keep in mind that
objects that are stored in a process-level cache should not be eagerly loaded
because that would bypass the cache.
In this section, you will learn how to improve performance of a Hibernate
application. We describe how to configure eager loading in Hibernate and exam-
ine some of the ways Hibernate's eager loading features interact with each other
and other Hibernate features. We also explain how to use process-level caching
and query caching, and we use the Place Order use case as an example. (We
aren't going to discuss how to disable lazy loading for a class or how to use the lazy
property mechanism we saw in section 6.2.2 because they are rarely used and have
some significant limitations.)
6.5.1
Using eager loading
One important way to improve the performance of a Hibernate application is to
use eager loading. By default, Hibernate lazily loads objects and collections and
uses a separate SQL SELECT statement for each object or collection. By enabling
eager loading, you can configure Hibernate to load related objects with a single
SQL SELECT . You can enable eager loading for a relationship either statically in the
O/R mapping or dynamically in a query by using what is called a fetch join. You
can use both approaches simultaneously, although Hibernate HQL queries ignore
the static settings.
Statically configuring eager loading
One way to configure eager loading for a relationship is in the O/R mapping. In the
mapping for a relationship, you can specify that Hibernate should always eagerly
load the related object or objects when the referencing object is loaded. You con-
figure eager loading for a relationship by specifying a value for the fetch attribute
of the relationship's mapping element. This attribute can have one of two values:
select : Lazily load the referenced object or collection with a separate SQL
SELECT statement. This is the default.
join : Eagerly load the referenced object or collection using an outer join.
 
 
 
 
Search WWH ::




Custom Search