{
return myPC;
}
attaching = true;
PersistenceCapable detachedPC = (PersistenceCapable)obj;
try
{
// Check if the object is already persisted
boolean persistent = false;
if (embedded)
{
persistent = true;
}
else
{
if (!myOM.getOMFContext().getPersistenceConfiguration().getBooleanProperty("org.jpox.attachSameDatastore"))
{
// We cant assume that this object was detached from this datastore so we check it
try
{
getStoreManager().getPersistenceHandler().locateObject(this);
persistent = true;
}
catch (JPOXObjectNotFoundException onfe)
{
// Not currently present!
}
}
else
{
// Assumed detached from this datastore
persistent = true;
}
}
// Call any "pre-attach" listeners
getCallbackHandler().preAttach(detachedPC);
if (myOM.getApiAdapter().isDeleted(detachedPC))
{
// The detached object has been deleted
myLC = myLC.transitionDeletePersistent(this);
}
if (!myOM.getTransaction().getOptimistic() &&
(myLC == myOM.getApiAdapter().getLifeCycleState(LifeCycleState.HOLLOW) ||
myLC == myOM.getApiAdapter().getLifeCycleState(LifeCycleState.P_NONTRANS)))
{
// Pessimistic txns and in HOLLOW/P_NONTRANS, so move to P_CLEAN
// TODO Move this into the lifecycle state classes as a "transitionAttach"
myLC = myLC.transitionMakeTransactional(this);
}
if (persistent)
{
// Make sure that all non-container SCO fields are loaded so we can make valid dirty checks
// for whether these fields have been updated whilst detached. The detached object doesnt know if the contents
// have been changed.
loadSCONonContainerFields();
}
// Add a state manager to the detached PC so that we can retrieve its detached state
JDOStateManagerImpl smDetachedPC = new JDOStateManagerImpl(myOM, cmd);
smDetachedPC.initialiseForDetached(detachedPC, getExternalObjectId(detachedPC), null);
detachedPC.jdoReplaceStateManager(smDetachedPC);
// Cross-reference the attached and detached objects for the attach process
smDetachedPC.referencedPC = myPC;
this.referencedPC = detachedPC;
// Retrieve the updated values from the detached object
retrieveDetachState(smDetachedPC);
if (!persistent)
{
// Object is not yet persisted! so make it persistent
// Make sure all field values in the attach object are ready for inserts (but dont trigger any cascade attaches)
internalAttachCopy(this, smDetachedPC, smDetachedPC.loadedFields, smDetachedPC.dirtyFields, persistent,
smDetachedPC.myVersion, false);
makePersistent();
}
// Go through all related fields and attach them (including relationships)
internalAttachCopy(this, smDetachedPC, smDetachedPC.loadedFields, smDetachedPC.dirtyFields, persistent,
smDetachedPC.myVersion, true);
// Remove the state manager from the detached PC
detachedPC.jdoReplaceStateManager(null);
// Remove the corss-referencing now we have finished the attach process
smDetachedPC.referencedPC = null;
this.referencedPC = null;