Hi Chris,I've also thought that if we restricted the usage to fresh PersistenceManagers we could attach at will. Some comments below.
On Aug 7, 2007, at 11:52 AM, cbeams wrote:
On Aug 7, 2007, at 11:35 AM, Andy Jefferson wrote:You could allow something of this form by imposing a restriction that if an object needs attaching, and that "AttachOnPersist" flag is set, and the PM already has a persistent object with that id then throw a JDOUserException. Clearly the primary use-case is where the user detaches a single object, andthen wants to attach the same thing, so they typically never hit the duplicate detached object issue.You're right about the primary use case. With this restriction in place, however, would I be able to do the following?import static javax.jdo.JDOHelper.*; assert pm.getAttachOnPersist() == true; assert pm.getDetachAllOnCommit() == true; // fetch a detched object Query q = pm.newQuery(Foo.class); q.setUnique(true); Foo foo = (Foo) q.execute();
As pointed out later, you need to commit() here to detach the instance.
// my foo object is now detached-clean
assert isDetached(foo) && !isDirty(foo);
// make it dirty:
foo.setBar("bar");
// attach
pm.currentTransaction().begin();
pm.makePersistent(foo);
pm.currentTransaction().commit();
foo is no longer known to pm because of DetachAllOnCommit.
// should be transitioned back to detached-clean
assert isDetached(foo) && !isDirty(foo);
// dirty it once more
foo.addBaz("baz");
// and attach it a second time - wouldn't this cause the
JDOUserException you mention above?
No.
The pm is already managing (by ID, I assume) this object, so how would it know the difference between the application 'detaching and attaching two separate instances of the same object' and simply 'attaching twice the same instance of an object'? Hopefully I'm missing something, because I like the proposed solution.
If you use detachAllOnCommit, commit essentially clears the cache so the instance can be attached again. In this case, the instance itself is detached, unlike serialization and detachCopy which leave the instance associated with the pm but give you a detached instance.
Craig
pm.currentTransaction().begin(); pm.makePersistent(foo); pm.currentTransaction().commit(); // again, should now be detached-clean assert isDetached(foo) && !isDirty(foo); - Chris
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!
smime.p7s
Description: S/MIME cryptographic signature
