Home » Support » Issue Tracking » After using the enhancer, Lazy loaded collections are no longer loading. They are set as null

After using the enhancer, Lazy loaded collections are no longer loading. They are set as null

Type: BugVersion: 2.5.7Priority: HighStatus: ClosedReplies: 1
#1

Environment: Mac OS X

JVM: Java 1.8 u 20

Steps to reproduce

Use the following classes:

/**

Account Class

**/

@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@uuid")
@EntityListeners(AbstractEntity.AbstractEntityListener.class)
public class Account extends AbstractEntity implements IAbstractEntity<Long>
{
    @Id
    @Column(name = "account_id")
    @GeneratedValue(strategy= GenerationType.TABLE)
    protected Long id;

    @Override
    public Long getId()
    {
        return id;
    }

    @Override
    public void setId(Long id)
    {
        this.id = id;
    }

    @OneToMany(cascade={CascadeType.MERGE, CascadeType.PERSIST}, mappedBy="account", fetch = FetchType.LAZY)
    @JoinColumn(name = "project_id", unique=true, nullable=false, updatable=false)
    protected List<Project> projects = new ArrayList<>();

    public List<Project> getProjects() {
        return projects;
    }

    public void setProjects(List<Project> projects) {
        this.projects = projects;
    }

}


/**
* Project Class
*/
@Entity
public class Project extends AbstractEntity implements IAbstractEntity<Long> {

    @Id
    @Column(name = "project_id")
    @GeneratedValue(strategy= GenerationType.TABLE)
    protected Long id;

    @Override
    public Long getId()
    {
        return id;
    }

    @Override
    public void setId(Long id)
    {
        this.id = id;
    }

    @ManyToOne
    @JoinColumn(name="account_id", nullable=false)
    public Account account;

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }
}


/**

Workflow

**/

Account account = new Account();

entityManager.persist(account);

entityManager.flush();

Project project = new Project();

project.setAccount(account);

entityManager.persist(project);

entityManager.flush();

account = entityManager.find(account);

assertNonNull(account.getProjects());

 

Expected Result,

 

account.getProjects() is not null and should contain a project

 

Note:  This scenario was working until running the enhancer on compiled jpa entities

 

#2

Your issue cannot be reproduced, since the posted code is not runnable. Particularly the invocation of find in your code does not compile. The following code included an attempt to reproduce your report. Please adjust it to a program that produces different results with and without enhancement.

import java.util.*;

import javax.persistence.*;

public class T1535 {

    public static void main(String[] args) throws Exception {
        EntityManagerFactory emf =
            Persistence.createEntityManagerFactory(
                "objectdb:db/test.tmp;drop");
        EntityManager em = emf.createEntityManager();
       
        Account account = new Account();
        em.persist(account);
        em.flush();

        Project project = new Project();
        project.setAccount(account);
        em.persist(project);
        em.flush();

        account = em.find(Account.class, 1);
        System.out.println(account.getProjects());

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

    @Entity
    public static class Account
    {
        @Id
        protected Long id = 1L;

        @OneToMany(cascade={CascadeType.MERGE, CascadeType.PERSIST},
            mappedBy="account", fetch = FetchType.LAZY)
        protected List<Project> projects = new ArrayList<Project>();

        public List<Project> getProjects() {
            return projects;
        }

        public void setProjects(List<Project> projects) {
            this.projects = projects;
        }
    }
   
    @Entity
    public static class Project {

        @Id
        protected Long id = 1L;

        @ManyToOne
        public Account account;

        public Account getAccount() {
            return account;
        }

        public void setAccount(Account account) {
            this.account = account;
        }
    }
}
ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

Post Reply

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