Java Reference
In-Depth Information
13.4.2
Implementing the persistent domain class
The Order class has accept() and reject() methods that update a status field that
tracks where the order is in the delivery process:
class Order {
private int version; // Hibernate only
private String state;
public boolean isAcknowledgable() {
return state.equals(SENT);
}
public void accept(String notes) {
if (!isAcknowledgable())
throw new ApplicationError();
this.state = ACCEPTED;
this.notes = notes;
}
public void reject(String notes) {
if (!isAcknowledgable())
throw new ApplicationError();
this.state = REJECTED;
this.notes = notes;
}
The Hibernate version of this class also has a version field, which is optional when
using JDO .
The isAcknowledgable() method returns true if the order can be acknowl-
edged. The accept() method updates the order with the notes and changes its
state to ACCEPTED . The reject() method is similar—it changes the state to
REJECTED . The accept() and reject() methods both throw an exception if the
order is in the wrong state to be acknowledged.
Because Order is a persistent class, we also need to define its O/R mapping.
Let's see how to do this for JDO and Hibernate.
JDO configuration
In the JDO ORM metadata for the Order class, we must specify that it uses optimis-
tic locking and that it is detachable. In addition, since the line items and restau-
rants are returned to the presentation tier we must also configure those classes to
be detachable. Here is an excerpt of the JDO 2.0 ORM metadata that configures
the Order class:
 
 
 
Search WWH ::




Custom Search