11.2 State Interrogation

The JDOHelper class provides the following methods to interrogate the state of an instance:

static boolean isPersistent(Object obj);
static boolean isTransactional(Object obj);
static boolean isDirty(Object obj);
static boolean isNew(Object obj);
static boolean isDeleted(Object obj);

Each of these methods returns false if the instance is null, transient, or of a class that is not persistent. Otherwise, these methods return the following:

isPersistent( )

Returns true for an instance that represents a persistent object in the datastore

isTransactional( )

Returns true for an instance whose state is associated with the current transaction

isDirty( )

Returns true for an instance whose state has changed in the current transaction

isNew( )

Returns true for an instance made persistent in the current transaction

isDeleted( )

Returns true if the instance has been deleted in the current transaction

Table 11-1 specifies the values these methods return for each required lifecycle state. You could write a method that calls each of these methods and returns a String denoting the instance's lifecycle state. This can be useful if you are debugging or would like to know the lifecycle state of instances.

Table 11-1. State interrogation method return values

State of Instance

isPersistent( )

isTransactional( )

isDirty( )

isNew( )

isDeleted( )

Transient

false
false
false
false
false

Hollow

true
false
false
false
false

Persistent-new

true
true
true
true
false

Persistent-clean

true
true
false
false
false

Persistent-dirty

true
true
true
false
false

Persistent-deleted

true
true
true
false
true

Persistent-new-deleted

true
true
true
true
true

Table A-1 in Appendix A provides a complete listing of the values these methods return for all the lifecycle states.