During the persistence process an object goes through lifecycle changes.
Below we demonstrate the primary object lifecycle changes for JPA. With JPA these lifecycles
are referred to as "persistence contexts". There are two :
transaction
(default for
J2EE usage) and
extended
(default for J2SE usage). DataNucleus allows control over which
to use by specification of the persistence property
datanucleus.jpa.persistenceContextType
A newly created object is
transient
. You then persist it and it becomes
persistent
. You then commit the transaction and it is detached for use elsewhere
in the application, in
detached
state. You then attach any changes back to
persistence and it becomes
persistent
again. Finally when you delete the object
from persistence and commit that transaction it is in
transient
state.
So a newly created object is
transient
. You then persist it and it becomes
persistent
. You then commit the transaction and it remains managed in
persistent
state. When you close the EntityManager it becomes
detached
.
Finally when you delete the object from persistence and commit that transaction it
is in
transient
state.
JPA provides nothing to determine the lifecycle state of an object.
Fortunately DataNucleus does consider this useful, so you can call the following
String state = NucleusJPAHelper.getObjectState(entity);
boolean detached = NucleusJPAHelper.isDetached(entity);
boolean persistent = NucleusJPAHelper.isPersistent(entity);
boolean deleted = NucleusJPAHelper.isDeleted(entity);
boolean transactional = NucleusJPAHelper.isTransactional(entity);
|