Craig, all,
This isn't so much a comment on the 2.1 spec changes, but rather a
suggestion for an addition to the JDOHelper interface in the 2.1
timeline (I don't know if it's too late for this sort of thing)...
JPOX's JPOXJDOHelper implementation exposes a very useful method with
the following signature:
String getObjectState(Object pc)
I would love to see this method exposed by the standard JDOHelper
interface. The spec currently provides status interrogation methods
(per section 8.5), but they're primarily useful assuming one is
testing for a particular state. For example, I might care to assert
that a pc is 'detached-dirty':
assert pc.isDetached() && pc.isDirty();
That's not so bad; it's even elegant. But if I'm debugging my way
through an application, there's no standardized way (that I know of)
to simply print out the state of the pc object. Using JPOX
internals, I can do the following:
logger.debug(JPOXJDOHelper.getObjectState(pc)); // prints out
'detached-dirty', 'transient-clean', or whatever the state actually is.
or, just as easily:
assert "detached-dirty".equals(JPOXJDOHelper.getObjectState(pc));
This method is most useful from a practical point of view:
debugging. One might argue that such code need not be portable and
thus standardized, but I'd counter with "why not"? State transitions
in JDO are complex enough; if we can make the standard interface for
interrogation of those states friendlier, all the better.
IMHO, the only problem with the current JPOX getObjectState()
implementation is that it's string-based. If this method were to be
considered for the 2.1 JDOHelper interface, I would suggest that the
return type be an enumeration that contains identifiers for all the
object states, optional and required:
package javax.jdo;
public interface JDOHelper {
// ...
ObjectState getObjectState(Object pc);
}
Where ObjectState is defined as:
package javax.jdo;
public enum ObjectState {
TRANSIENT,
TRANSIENT_CLEAN,
TRANSIENT_DIRTY,
PERSISTENT_NEW,
PERSISTENT_NONTRANSACTIONAL,
PERSISTENT_NONTRANSACTIONAL_DIRTY,
PERSISTENT_CLEAN,
PERSISTENT_DIRTY,
HOLLOW,
PERSISTENT_DELETED,
PERSISTENT_NEW_DELETED,
DETACHED_CLEAN,
DETACHED_DIRTY;
}
With this addition in place, the user gets the best of both worlds.
Asserting for a given state is trivially easy:
assert JDOHelper.getObjectState(pc) == ObjectState.DETACHED_DIRTY;
And determining object state with no a priori knowledge is simple, too:
logger.debug(JDOHelper.getObjectState(pc)); // prints out
"DETACHED_DIRTY"
The ObjectState enum could also override toString() such that it
transforms the all-caps-and-underscores identifier to the lower-case-
and-dashes format used within the spec, e.g.: DETACHED_DIRTY.toString
() returns "detached-dirty"
Thanks for considering,
- Chris Beams
From: Craig L Russell <[EMAIL PROTECTED]>
Date: August 3, 2007 7:04:52 PM PDT
To: Apache JDO project <[email protected]>, JDO Expert Group
<[EMAIL PROTECTED]>
Subject: JDO 2.1 specification draft can be reviewed...
at http://db.apache.org/jdo/documentation.html
Check it out, and send comments...
Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:[EMAIL PROTECTED]
P.S. A good JDO? O, Gasp!