Java Reference
In-Depth Information
This test corresponds to one possible scenario in the lifetime of a PendingOrder . In
order to thoroughly test PendingOrder , we would also need to write other tests, such
as one that calls updatePaymentInfo() with a Coupon . Developing these tests can be
time consuming, but they are an important part of the test suite for Hibernate per-
sistence layer. As with the O/R mapping tests, you can start off by writing a simple
test that creates and saves a PendingOrder and then add more comprehensive tests
over time. Let's look at what we have to do in order to get these tests to pass.
6.3.3
Making a class persistent
We have written the tests and thus have put behind us what is often the most diffi-
cult part of persisting a class. To get these tests to pass, we have to make some minor
changes to the PendingOrder class and write the O/R mapping document.
Changing the class
To be able to persist a class, you typically have to make a few changes to accommo-
date Hibernate's requirements. For example, a class must have a default construc-
tor. Also, you usually have to add a field to store the object's persistent identity,
and some classes require a version field for optimistic locking. In addition, as
described in Hibernate in Action [Bauer 2005], you must—in certain situations—
implement equals() and hashCode() methods. Furthermore, even though Hiber-
nate provides a rich set of O/R mapping features, you sometimes have to make
changes to work around its limitations.
Fortunately, we only need to make some minor changes to the PendingOrder
class. The class already has a default constructor, and does not require a version
field because its session state is accessed by just one user. As a result, we only have
to add an id field and a getter for accessing it:
public class PendingOrder {
private int id = -1;
private int getId() {
return id;
}
The rest of the class is unchanged and it's still a POJO . As you can see, this is a very
simple change, which is one of the really nice things about using an ORM frame-
work such as Hibernate.
 
 
 
Search WWH ::




Custom Search