Java Reference
In-Depth Information
In most cases this is a simple change with only a minimal impact on the
domain model. It is also common to have such an abstract class already in place.
However, it's a shame that we need to introduce an abstract class because inter-
faces are such a fundamental OO concept.
Let's now look at the
JDO
enhancer tool and its impact on the edit-compile-
debug cycle.
5.1.3
Using the JDO enhancer
Cirque du Soleil's Mystere at Treasure Island in Las Vegas is my favorite perfor-
mance of all time. The clowns and acrobats perform one breathtaking act after
another. They appear to defy gravity and do things that do not seem humanly pos-
sible. Making it look so effortless requires a tremendous amount of hard work,
countless backstage people, and large amounts of technology.
Implementing persistence transparently is also a difficult problem. In order to
make it seem effortless,
ORM
frameworks such as
JDO
must perform “behind the
scenes” magic to implement features such as lazy loading and change tracking.
Consider, for example, what happens when the application executes the following
code snippet that loads a
PendingOrder
and navigates to its restaurant:
String idString = "555";
IntIdentity objectId = new IntIdentity(PendingOrder.class, idString);
PendingOrder p = (PendingOrder)pm.getObjectById(objectId);
String restaurantName p = p.getRestaurant().getName()
The
JDO
implementation must provide the illusion that the
Restaurant
object is
in memory even when it is loaded lazily. The
JDO
implementation must also keep
track of changes to objects so that it can update the database.
There is more than one way to implement transparent persistence, but the
approach taken by most
JDO
implementations is to use a bytecode enhancer. The
bytecode enhancer
is a tool that must be run on the persistent classes and any classes
that directly access their fields. It reads the
XML
metadata defining the object/
relational mapping and changes the bytecodes in each class file to implement
features such as lazy loading and change tracking. The enhancer, which was man-
datory in
JDO 1.0
, became optional in
JDO 2.0
, but it is likely that it will continue
to be the way
JDO
implementations provide transparent persistence. One of its
valuable benefits is that
JDO
objects, unlike Hibernate objects, work with
instanceof
and can be downcasted.
One drawback of using the enhancer is that it's an extra step in the edit-com-
pile-debug cycle. Before the application or its tests can be executed, you must run
the bytecode enhancer, either from within your
IDE
or by using Ant, on any newly
compiled class files. This can be a problem when you're working within an
IDE
Search WWH ::
Custom Search