"org.hibernate.LazyInitializationException: could not initialize proxy - no Session" in EAP 6

Solution Verified - Updated -

Issue

  • An entity is defined with a LAZY fetch ManyToOne association
@Entity
public class Member implements Serializable {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "phone_number")
    private PhoneNumber phoneNumber;
    ...
}
  • An instance of the entity type is retrieved by a criteria query outside a transaction where both join and fetch are used
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Member> criteria = cb.createQuery(Member.class);
        Root<Member> member = criteria.from(Member.class);
        member.fetch("phoneNumber", JoinType.INNER);
        member.join("phoneNumber"); // join seems to break aggressive fetch
        ...
        Member m = em.createQuery(criteria).getSingleResult();
        PhoneNumber n = m.getPhoneNumber();
  • LazyInitializationException is raised when accessing a property of the associated entity
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:165)
    org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:286)
    org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
    org.test.model.PhoneNumber_$$_jvst471_3.getPhoneNumberValue(PhoneNumber_$$_jvst471_3.java)
    org.test.MemberRepository.findByEmail(MemberRepository.java:49)
  • The exception did not occur in EAP 6.3.0 but has been observed in later EAP 6.3.x cumulative patches and in 6.4.0

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP) 6
  • Hibernate 4

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content