Java Reference
In-Depth Information
when persisting a domain model class with Hibernate. You will also learn how to
implement some of the testing techniques described in chapter 4, including how
to write tests that use the Hibernate metadata API s to validate the O/R mapping.
In the following section, we show how to implement a domain model repository
using Hibernate.
The PendingOrder class is a good example of a domain model entity. To persist
this class we will need to use a variety of Hibernate's ORM features. The Pending-
Order class has simple fields such as deliveryTime and state that need to be
mapped to columns of the PENDING_ORDER table. It has also has fields that refer-
ence embedded value objects such as paymentInformation and deliveryAddress
that must also be mapped to columns in the PENDING_ORDER table. It also has ref-
erences to persistent objects stored in other tables, including PendingOrderLine-
Items , Restaurant , and Coupon . The reference to a Coupon is a polymorphic
reference because Coupon is an interface.
But before getting into the details of writing the O/R mapping for this class,
let's first look at how to write tests that verify that the persistent PendingOrder
objects can be created, loaded, updated, and deleted.
6.3.1
Writing Hibernate persistence tests with ORMUnit
Because we are using test-driven development, the first step in the process of mak-
ing the PendingOrder class persistent is to write some tests using the testing strate-
gies described in chapter 4. There are three different kinds of tests that we need
to write for a class such as PendingOrder :
Tests that verify that the O/R mapping correctly maps the PendingOrder
class to the database schema. This includes making sure that all fields that
should be persistent are mapped to the database.
1
Tests that verify that instances of the PendingOrder class can be created,
loaded, updated, and deleted. Sometimes, for example, incorrectly
defined database constraints prevent objects from being persisted.
2
Tests that verify that the tables and columns referenced by the Pending-
Order 's O/R mapping exist.
3
The ORMU nit test framework, which we introduced in chapter 4, provides Hiber-
nateMappingTests , HibernateSchemaTests , and HibernatePersistenceTests , which
are base classes that make it easier to write Hibernate persistence tests. Let's see how
to use these classes.
 
 
Search WWH ::




Custom Search