JDOHelper.isDetached failure ?

#1

Running this example code:

    public static void main(String[] args) {
        PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("test.odb");
        PersistenceManager pm = pmf.getPersistenceManager();
        pm.currentTransaction().begin();
        TestClass test = new TestClass(1234,"test");
        pm.makePersistent(test);
        System.out.println(JDOHelper.isPersistent(test));
        pm.currentTransaction().commit();
        List<TestClass> list = (List<TestClass>) pm.newQuery(TestClass.class).execute();
        TestClass first = list.get(0);
        TestClass detached = pm.detachCopy(first);
        System.out.println(JDOHelper.isDetached(detached));
        pmf.close();
    }

With this as the persistent class:

@PersistenceCapable(detachable="true")
public class TestClass implements Serializable {
   
    @PrimaryKey
    private long id;
   
    private String name;
   
    protected TestClass() { }
   
    public TestClass(long id,String name) {
        this.id = id;
        this.name = name;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the id
     */
    public long getId() {
        return id;
    }
}

Prints out the following:

true
false

 

So it seems that the JDOHelper.isDetached method isn't doing what it should?

#2

The method JDOHelper.isDetached is one of the JDO features that are supported by ObjectDB only for enhanced classes. For a class that is not enhanced ObjectDB cannot tell the difference between a detached object and a transient object, so any detached object is considered as transient. It is not practical to support it for non enhanced classes, without causing a memory leak.

ObjectDB may be the only JDO implementation that supports non enhanced classes, but this support is not complete, and anyway it is always recommended to use enhanced classes, which are also more efficient.

Running your example with enhancement:

> java -javaagent:C:\objectdb\bin\objectdb.jar Test

gives the correct output:

true
true
ObjectDB Support
#3

That works for me.  Thanks again for your prompt response.

Reply