Issue #326: Navigation through lazy loading from Detached Objects

Type: Feature RequestVersion: 2.2.6Priority: NormalStatus: ActiveReplies: 4
#1

A main limitation of detached objects (as explained on the Detached Entities manual page) is:

  • Retrieval by navigation from detached objects is not supported, so only persistent fields that have been loaded before detachment should be used.

As explained on the Object DB vs EclipseLink/TopLink: on accessing lazy loaded relationships outside a transaction, after a query, from Glassfish forum thread - this JPA limitation is avoided in EclipseLink by an extension, which keep detached objects in a semi detached state (in which they are still associated with the EntityManagerFactory and enable  lazy loading) - until serialization, which completes the detachment.

It seems that implementing a similar extension in ObjectDB would ease emigration of web applications from EclipseLink to ObjectDB.

ObjectDB Support
#2

Just an update to confirm my very strong interest in this feature.
We simply can't use our EclipseLink-based JavaEE (JPA+JSF) web application with ObjectDB otherwise.

We are very interested in using ObjectDB technology after a very promising evaluation (thanks for that)
but for us the inability to navigate lazy relationships of detached entities is a big showstopper,
as we rely on that capability heavily in our web application, especially from JSF, pulling very complex
data and relationships as and when needed. We do not want to write 1000s of dedicated projection queries 
and/or fetch joins that can preempty data that will be needed in the user interface.

[Recommended reading JPA Pro 2:

- p.164 to p.168 Working with Detached Entities

- p.222 Fetch Joins

]

We appreciate that this feature, available in some other JPA providers, goes beyond the requirements and spec of JPA2.

It will however make it much easier for many people who have coded against EclipseLink to migrate to ObjectDB.

thanks,

Webel

PS: have upgrade to critical because for us it is, we simply can't use ObjectDB for our system as coded.

#3

Build 2.3.6_16 includes an initial attempt to provide some sort of basic support of this feature.

A new system property ("objectdb.temp.no-detach") can be set to disable detachment of objects. Basically that is what EclipseLink does. Entity objects are not really detached when using EclipseLink.

The following test demonstrates how it works:

import java.util.*;

import javax.persistence.*;

public final class T326 {

    public static void main(String[] args) {

        // System.setProperty("objectdb.temp.no-detach", "true");

        EntityManagerFactory emf =
            Persistence.createEntityManagerFactory(
                "objectdb:$objectdb/test.tmp;drop");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        MyEntity e1 = new MyEntity(1, "e1");
        MyEntity e2 = new MyEntity(2, "e2");
        e1.list = Collections.singletonList(e2);
        em.persist(e1);
        em.getTransaction().commit();
        System.out.println(e1);
        em.close();
        emf.close();
       
        emf = Persistence.createEntityManagerFactory(
            "objectdb:$objectdb/test.tmp");
        em = emf.createEntityManager();
        e1 = em.find(MyEntity.class, 1);
        em.close();
        emf.close();
        System.out.println(e1);
    }
   
    @Entity
    static final class MyEntity {
        @Id int id;
        String str;
       
        @OneToMany(cascade=CascadeType.PERSIST)
        List<MyEntity> list;
       
        MyEntity() {
        }
       
        MyEntity(int id, String str) {
            this.id = id;
            this.str = str;
        }

        @Override
        public String toString() {
            return id + "->" + str + ":" + list;
        }
    }
}

When the line that sets the system property is commented the output is:

1->e1:[2->e2:null]

1->e1:[]

and when that line is active the output is:

1->e1:[2->e2:null]

1->e1:[2->e2:null]

This is by no way a complete solution. It is merely disables detachment. The effect is unclear yet, but for example, it could affect memory consumption and file closing if detached objects (which are not detached anymore) have long life.

So feedback is needed.

ObjectDB Support
#4

confirming have read and will try, thanks, Webel.

#5

Build 2.3.7_01 fixes NullPointerException in accessing inverse (mapped by) fields after detachment.

ObjectDB Support

Reply