LazyInitializationException: Unable to perform requested lazy initialization ... no session and settings disallow loading outside the Session in Hibernate

Solution Verified - Updated -

Issue

  • When accessing a (@OneToMany annotated) lazy association (from an Employee entity to a list of Task entities) an exception is raised

    ... org.hibernate.LazyInitializationException: Unable to perform requested lazy initialization [support.hibernate.entity.Employee.tasks] - no session and settings disallow loading outside the Session
        at org.hibernate.bytecode.enhance.spi.interceptor.Helper.throwLazyInitializationException(Helper.java:165)
        at org.hibernate.bytecode.enhance.spi.interceptor.Helper.performWork(Helper.java:60)
        at org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor.loadAttribute(LazyAttributeLoadingInterceptor.java:76)
        at org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor.fetchAttribute(LazyAttributeLoadingInterceptor.java:72)
        at org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor.intercept(LazyAttributeLoadingInterceptor.java:61)
        at org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor.readObject(LazyAttributeLoadingInterceptor.java:296)
        at support.hibernate.entity.Employee.$$_hibernate_read_tasks(Employee.java)
        at support.hibernate.entity.Employee.getTasks(Employee.java:49)
        ...
    

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP) 7
  • Hibernate 5

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In
Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.