"org.hibernate.LazyInitializationException: could not initialize proxy - no Session" in EAP 6
Issue
- An entity is defined with a
LAZYfetchManyToOneassociation
@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
joinandfetchare 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();
LazyInitializationExceptionis 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 of over 48,000 articles and solutions.
Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.
