Does ObjectDB support lazy loading?

#1

I'm evaluating ObjectDB 2.0 RC1. Everything works fine except a lazy loading problem.

I've a Parent class which contains List<Child>, marked with @OneToMany. After persisting a Parent object, (in the Explorer) I can see all the property values of the Child objects in the list. However, when the Parent object is read from EntityManager, all the properties except ID of the Child objects are null. The EntityManager is not yet closed when the getters are called.

After I change the annotation to @OneToMany(fetch=FetchType.EAGER), the properties can be read. It seems to me that ObjectDB doesn't support lazy loading, am I right?

 

#2

ObjectDB does support both lazy and eager loading. If you found a problem in a specific situation - please provide a simple test program that demonstrates that issue.

The following sample program demonstrates lazy loading:

package com.objectdb.test;

import java.util.*;

import javax.persistence.*;

public final class LazyTest
{
    public static void main(String[] args)  
    { 
        String path = "test.odb";
        EntityManagerFactory emf = Persistence.createEntityManagerFactory(path);

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        A a = new A();
        a.list = new ArrayList<B>();
        a.list.add(new B(1));
        a.list.add(new B(2));
        em.persist(a);
        em.getTransaction().commit();
        Long id = a.id;
        em.close();

        em = emf.createEntityManager();
        a = em.find(A.class, id);
        System.out.println(a.list);
        em.close();

        emf.close();
    }
 
    @Entity
    static class A {
        @Id @GeneratedValue Long id;
        @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL) List<B> list;
    }

    @Entity
    static class B {
        @Id @GeneratedValue public Long id;
        int value;
        B(int value) { this.value = value; }
        @Override public String toString() { return "B(" + value + ")"; }
    }
}
ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#3

Thanks for your quick response. Your example works, but then I change the main method as follow, and execute

java ... LazyTest persist

java ... LazyTest find

The find prints out [B(0), B(0)]. Why it fails after the DB is re-opened?

public static void main(String[] args)  
{ 
    String path = "test.odb";
    EntityManagerFactory emf = Persistence.createEntityManagerFactory(path);
 
    EntityManager em = emf.createEntityManager();
        
    if (args[0].equals("persist")) {
         em.getTransaction().begin();
         A a = new A();
         a.list = new ArrayList<B>();
         a.list.add(new B(1));
         a.list.add(new B(2));
         em.persist(a);
         em.getTransaction().commit();
         Long id = a.id;
         System.out.println(id);
    } else if (args[0].equals("find")) {
         A a = em.find(A.class, 1);
         System.out.println(a.list); 
    }
        
    em.close();
    emf.close();
}
#4

Thank you for your test code - it seems that there was a bug in lazy loading of instances of non enhanced classes.

Please try build 09 that should fix this issue.

In addition, you might want to enhance your entity classes to improve performance.

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

The first of the LazyTest programs above prints [B(0), B(0)] if the classes are not enhanced. I am using objectdb-2.0-RC4_04.

#6

You are right, thank you for the report. Please check the new build that should fix it.

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

Post Reply

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