Java Reference
In-Depth Information
6.6 Summary
Hibernate provides mostly transparent persistence for
POJO
classes. It provides a
rich
ORM
mechanism that makes it quite easy to persist a domain model such as the
one for the Place Order use case. Its features include embedded value objects, inher-
itance, and automatic deletion of orphaned children in a parent/child relationship.
Despite its power, Hibernate has several important limitations that can impact
the design of an application. One limitation is inefficient handling of unidirec-
tional one-to-many relationships that are mapped using a foreign key. Another
limitation is that lazily loaded objects do not support
instanceof
and downcast-
ing. In addition, recovering from an error when a
HibernateException
was
thrown can be difficult. Another challenge is dynamically configuring eager load-
ing on a per-request basis.
You can use a test-driven approach to develop a Hibernate persistence layer.
There are three different kinds of tests you can write for each persistent class.
First, you can write a test that uses the Hibernate metadata
API
s to verify that the
XML
mapping document correctly implements the
O/R
mapping for the class.
Second, you can write persistence tests that verify that instances of the class can be
saved, updated, and deleted. Finally, you can write a test that that verifies that the
schema matches the
O/R
mapping. For each repository, you write mock object
tests that verify that the repository calls the Hibernate/Spring
API
s correctly. You
can also write database tests for the queries that are called by the repositories.
Now that we have looked at how to use Hibernate and
JDO
to persist a domain
model, the next step is to decide what kind of interface the business logic exposes
to the presentation. One option is to encapsulate the domain model with a
POJO
façade that uses Spring for transaction management, as you'll see in the next chapter.
Search WWH ::
Custom Search