PostUpdate collection null

#1

Hello,

I have an entity MyEntity with a collection attribute myCollection. I have an EntityListener on MyEntity with a @PostUpdate method. When I try to access myCollection from the PostUpdate method, this collection is null. If I try to access the collection from the PreUpdate method, the collection is populated. The collection is eagerly fetched.

public class MyEntity {

    @OneToMany(fetch=FetchType.EAGER)

    private ArrayList<OtherEntity> myCollection = new ArrayList<OtherEntity>();

}


Thanks,

farid

#2

It is very difficult to provide help without a test case that demonstrates the problem.

Please look at the following test case:

import java.util.*;

import javax.persistence.*;


public final class T1473
{
    static java.sql.Date myDate;

    public static void main(String[] args) {

        EntityManagerFactory emf =
            Persistence.createEntityManagerFactory(
                "objectdb:$objectdb/db/test.tmp;drop");

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        MyEntity e = new MyEntity(1);
        e.myCollection.add(new MyEntity(2));
        e.myCollection.add(new MyEntity(3));
        em.persist(e);
        em.getTransaction().commit();
        em.close();

        em = emf.createEntityManager();
        e = em.find(MyEntity.class, 1);
        em.getTransaction().begin();
        e.myCollection.add(new MyEntity(4));
        em.getTransaction().commit();

        em.close();
        emf.close();
    }

    @Entity
    public static class MyEntity {
        @Id int id;
       
        @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.PERSIST)
        List<MyEntity> myCollection = new ArrayList<MyEntity>();
       
        MyEntity(int id) {
            this.id = id;
        }
       
        @PostUpdate
        public void postUpdate() {
            System.out.println(myCollection.size());
        }
    }
}

The postUpdate method is invoked as expected and the output is 3.

You may try to modify this test in order the demonstrate the issue that you have.

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

Post Reply

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