Java Reference
In-Depth Information
coupon field. Hibernate will instantiate the appropriate subclass of Coupon and the
application will be able to use instanceof and downcasting.
Using lazy property fetching to implement lazy loading of related objects has
two main drawbacks. First, it is a lot less convenient than using proxies because
you have to run the bytecode enhancer, which is an extra step in the edit-compile-
debug cycle.
Second, it is less efficient because lazily loaded properties are retrieved from
the database one at time, Moreover, Hibernate can use inefficient SQL to load
each property. For example, when loading a coupon Hibernate uses an additional
SQL statement that loads the COUPON_ID foreign key column from the
PENDING_ORDER table. Here are the SQL statements that Hibernate uses to load
the PendingOrder and its Coupon :
select pendingord0_....
from PENDING_ORDER pendingord0_
where pendingord0_.PENDING_ORDER_ID=?
select pendingord_.COUPON_ID as COUPON11_1_
from PENDING_ORDER pendingord_
where pendingord_.PENDING_ORDER_ID=?
select abstractco0_...
from COUPON abstractco0_
where abstractco0_.COUPON_ID=?
The first SQL SELECT statement retrieves all of the columns from the
PENDING_ORDER table except for the COUPON_ID column, which is the foreign key
to the COUPON table. The second statement, which is executed when the applica-
tion accesses the coupon property, retrieves the COUPON_ID foreign key column.
The third SQL SELECT statement loads the coupon. In comparison, if Hibernate
was configured to use proxies it would only use two SQL SELECT statements, one
for the PendingOrder and another for the Coupon .
Because of these problems, it is usually much better to use Hibernate's proxy-
based mechanism for lazily loading and to work around the problems with
instanceof and downcasting.
6.3 Persisting a domain model class using Hibernate
We have now seen the issues and challenges you will face when using Hibernate to
persist a domain model, so let's look at an example. In this section, we implement
the Hibernate O/R mapping for the PendingOrder class from the Food to Go
domain model. It illustrates some of the typical issues that you will encounter
 
 
Search WWH ::




Custom Search