JDOHelper.isDetached failure ?


Running this example code:

    public static void main(String[] args) {
        PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("test.odb");
        PersistenceManager pm = pmf.getPersistenceManager();
        TestClass test = new TestClass(1234,"test");
        List<TestClass> list = (List<TestClass>) pm.newQuery(TestClass.class).execute();
        TestClass first = list.get(0);
        TestClass detached = pm.detachCopy(first);

With this as the persistent class:

public class TestClass implements Serializable {
    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:



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


The method JDOHelper.isDetachedisDetached(pc)JDOHelper's static methodTests whether the parameter instance has been detached.See JavaDoc Reference Page... 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:

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

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

Post Reply

To post a reply and/or subscribe to update notifications - please login