JDO PersistenceManager: how can I tell that an object returned by getObjectById has been deleted?
In my app I have a certain control flow that goes like this:
DAO object = persistenceManager.getObjectById(DAO.class, id);
...
persistenceManager.deletePersistent(anotherReferenceToObject);
...
DAO aThirdObjectReference = persistenceManager.getObjectById(DAO.class, id);
These are all in different scopes, but the persistenceManager
references all point to the same PersistenceManager
.
The problem comes in when the third DAO object turns out to be the same as the second object. The PersistenceManager
happily returns the same object that was just deleted, but I need to know it was deleted before I try accessing or changing parts of it!
I don't want to close the pm because it's important for other reasons to keep it open.开发者_Go百科 Is there any way to tell if an object has been the parameter to deletePersistent
?
Are you using PersistenceManager.flush() after you delete the object? Until that is called either programmatically or when the manager is closed, any changes (edits, creations, deletions) will not be committed to the database. Thus, "deleted" objects will be accessible in their original state from when the PersistenceManager was created/last flushed.
As DataNucleus pointed out, I can test for prior deletion with:
JDOHelper.getObjectState(object).equals(ObjectState.PERSISTENT_DELETED)
This will return true after object
has been deleted even if the transaction has not been committed and the pm has not been flushed.
精彩评论