users@glassfish.java.net

Lazy loading does not work w Glassfish 3.0.1 + EclipseLink

From: janne postilista <jannepostilistat_at_gmail.com>
Date: Tue, 14 Sep 2010 16:44:50 +0300

Hi,

I'm using Eclipselink to provide JPA. I'm using GlassFish 3.0.1.

I have a some simple Employer and Person entities with many-to-many
(using join table) and one-to-many relationships. I want to have the
relationships lazy loaded, but it looks like they are always eagerly
loaded. I am aware that lazy/eager is just a hint to the JPA
implementation, but I'm thinking that lazy loading is probably
supposed to work with Eclipselink?

Entities are along the lines of (likely irrelevant lines omitted):


@Entity
@NamedQueries({
    @NamedQuery(name = "Employer.findAllLatest2", query = "select o
from Employer o"),
})
public class Employer implements Serializable {
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
    @JoinTable(name = "EMPLOYER_PERSON", joinColumns =
    @JoinColumn(name = "EMPLOYER_ID"), inverseJoinColumns =
    @JoinColumn(name = "PERSON_ID"))
    private Set<Person> persons;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "BOSS_ID")
    private Person boss;
}

@Entity
@NamedQueries({
  @NamedQuery(name = "EmployerPerson.findAll", query = "select o from
EmployerPerson o")
})
@Table(name = "EMPLOYER_PERSON")
@IdClass(EmployerPersonPK.class)
public class EmployerPerson implements Serializable {
    @Id
    @Column(name="EMPLOYER_ID", nullable = false, insertable = false,
            updatable = false)
    private Long employerId;
    @Id
    @Column(name="PERSON_ID", nullable = false, insertable = false,
            updatable = false)
    private Long personId;
    @ManyToOne
    @JoinColumn(name = "EMPLOYER_ID")
    private Employer employer;
    @ManyToOne
    @JoinColumn(name = "PERSON_ID")
    private Person person;
}

@Entity
public class Person implements Serializable {
    @ManyToMany(mappedBy="persons")
    private Set<Employer> employers;

@Stateless
@LocalBean
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class EmployerService {

    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public List<Employer> findAll() {
        String namedQuery = "Employer.findAllLatest2";
        }
        List<Employer> employers = em.createNamedQuery(namedQuery,
Employer.class).getResultList();
        for (Employer e: employers) {
            em.detach(e);
        }
        return employers;
    }

Now, when I use findAll(), I would expect that Employer objects do not
have the person references fecthed. But they are. Accessing persons
succeeds even though I did add (extraneous) detach() -calls to make
sure the entities are detached after returning from findAll().