org.hibernate.LazyInitializationException

There are no available Samebug tips for this exception. Do you have an idea how to solve this issue? A short tip would help users who saw this issue last week.

  • I was using Spring 1.0 with Hibernate2 for my DAO layer. Xdoclet is used to generate hibernate mapping. Spring among other thing is also used to manage transaction (declarative). This is working perfectly. I decided to upgrade to Hiebernate3 and hence Spring 1.2 RC2. Had to upgrade couple of hibernate and spring jar's. Figured out some of the methods in hibernate temple are gone but overall the migration was seamless. The systems build and deployes. BUT When I deploy the webapp (in Tomcat 5.0 and JDK 1.4.2), and make a DAO call and get the entity and try to access some property within the entity I am getting this exception Any Suggestions. Looks like I am missing some kind of configuration. Thanks Gopal ================================= org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:80) at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134) at com.manheim.dms.entity.security.vo.Role$$EnhancerByCGLIB$$4ad14d28.getActive(<generated>) at com.manheim.dms.system.SecurityManager.buildProfile(SecurityManager.java:85) at com.manheim.dms.handler.AdminHandler.authenticate(AdminHandler.java:147) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:324) at com.cysive.framework.servlet.controller.ProxyRequestHandler.processRequest(ProxyRequestHandler.java:104) at com.cysive.framework.servlet.controller.Dispatcher.processAction(Dispatcher.java:1040) at com.cysive.framework.servlet.controller.Dispatcher.processRequest(Dispatcher.java:1081) at com.cysive.framework.servlet.controller.Dispatcher.doPost(Dispatcher.java:305) at javax.servlet.http.HttpServlet.service(HttpServlet.java:709) at javax.servlet.http.HttpServlet.service(HttpServlet.java:802) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214) at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520) at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152) at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137) at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118) at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520) at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929) at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705) at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577) ================================
    via by Gopal Koratana,
  • I was using Spring 1.0 with Hibernate2 for my DAO layer. Xdoclet is used to generate hibernate mapping. Spring among other thing is also used to manage transaction (declarative). This is working perfectly. I decided to upgrade to Hiebernate3 and hence Spring 1.2 RC2. Had to upgrade couple of hibernate and spring jar's. Figured out some of the methods in hibernate temple are gone but overall the migration was seamless. The systems build and deployes. BUT When I deploy the webapp (in Tomcat 5.0 and JDK 1.4.2), and make a DAO call and get the entity and try to access some property within the entity I am getting this exception Any Suggestions. Looks like I am missing some kind of configuration. Thanks Gopal ================================= org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:80) at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134) at com.manheim.dms.entity.security.vo.Role$$EnhancerByCGLIB$$4ad14d28.getActive(<generated>) at com.manheim.dms.system.SecurityManager.buildProfile(SecurityManager.java:85) at com.manheim.dms.handler.AdminHandler.authenticate(AdminHandler.java:147) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:324) at com.cysive.framework.servlet.controller.ProxyRequestHandler.processRequest(ProxyRequestHandler.java:104) at com.cysive.framework.servlet.controller.Dispatcher.processAction(Dispatcher.java:1040) at com.cysive.framework.servlet.controller.Dispatcher.processRequest(Dispatcher.java:1081) at com.cysive.framework.servlet.controller.Dispatcher.doPost(Dispatcher.java:305) at javax.servlet.http.HttpServlet.service(HttpServlet.java:709) at javax.servlet.http.HttpServlet.service(HttpServlet.java:802) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214) at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520) at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152) at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137) at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118) at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520) at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929) at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705) at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577) ================================
    via by Gopal Koratana,
  • Hibernate / Mailing Lists
    via by Unknown author,
  • JSF and Hibernate problem
    via by 843842,
  • I have two domain objects - Employee and Department - and there is a 1:N relationship from Department to Employee. When I join fetch an Employee with its Department, without query cache enabled, it works fine. If I enable query cache for this same query, it bombs with a LazyInitializationException. Notes: 1) We get this error only if query cache is enabled. 2) We observed the same behaviour on both oscache and ehcache 3) Calling Hibernate.initialize() explicitly after firing the HQL seems to work. The initialization does not fire an extra query though (it seems to pick it from the cache). 4) Setting "lazy=false" on the "many-to-one" mapping also works. However, it wouldn't be acceptable. Hibernate version: 3.0.5 Mapping documents: Employee.hbm.xml <hibernate-mapping> <class name="tavant.platform.test.domain.Employee" table="CACHE_ISSUE_EMP" lazy="true" dynamic-update="true" dynamic-insert="true"> <cache usage="read-write" /> <id name="id" column="EMP_ID" type="java.lang.Long" access="field" unsaved-value="null"> <generator class="increment" /> </id> <property name="name" type="string" update="true" insert="true" column="EMP_NAME"/> <many-to-one name="department" class="tavant.platform.test.domain.Department" cascade="none" outer-join="auto" update="true" insert="true" column="DEPARTMENT_ID"/> </class> </hibernate-mapping> Department.hbm.xml <hibernate-mapping> <class name="tavant.platform.test.domain.Department" table="CACHE_ISSUE_DEP" lazy="true" dynamic-update="true" dynamic-insert="true"> <cache usage="read-write" /> <id name="id" column="DEPARTMENT_ID" type="java.lang.Long" access="field"> <generator class="increment"/> </id> <property name="name" type="java.lang.String" update="false" insert="true" column="NAME"/> <bag name="employees" lazy="true" inverse="true" cascade="save-update" access="field"> <cache usage="read-write"/> <key column="DEPARTMENT_ID"/> <one-to-many class="tavant.platform.test.domain.Employee"/> </bag> </class> </hibernate-mapping> Code between sessionFactory.openSession() and session.close(): public Employee getEmployeeWithDepartment(String empName) { Session session = null; try { session = sessionFactory.openSession(); Employee emp = (Employee) session.createQuery( "from Employee e join fetch e.department where e.name = :name") .setString("name", empName) .setCacheable(true) .uniqueResult(); // If I uncomment the next line, this works (even without // firing an extra query)! // Hibernate.initialize(emp.getDepartment()); return emp; } finally { if (session != null) { session.close(); } } } // First load employee and populate cahces Employee emp = test.getEmployeeWithDepartment(EMPLOYEE_NAME); System.out.println("Employee : " + emp + ", Employee.Department : " + emp.getDepartment()); // Now try to make use of the cache emp = test.getEmployeeWithDepartment(EMPLOYEE_NAME); System.out.println("Employee : " + emp + ", Employee.Department : " + emp.getDepartment()); Full stack trace of any exception that occurs: org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:84) at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134) at tavant.platform.test.domain.Department$$EnhancerByCGLIB$$67b26899.toString(<generated>) at java.lang.String.valueOf(String.java:2131) at java.lang.StringBuffer.append(StringBuffer.java:370) at tavant.platform.test.client.TestPrefetchRelationWithQueryCacheEnabled.main(TestPrefetchRelationWithQueryCacheEnabled.java:116) Name and version of the database you are using: We have noticed this on Oracle and HSQL The generated SQL (show_sql=true): #First read, goes fine Hibernate: select employee0_.EMP_ID as EMP1_0_, department1_.DEPARTMENT_ID as DEPARTMENT1_1_, employee0_.EMP_NAME as EMP2_1_0_, employee0_.DEPARTMENT_ID as DEPARTMENT3_1_0_, department1_.NAME as NAME0_1_ from CACHE_ISSUE_EMP employee0_ inner join CACHE_ISSUE_DEP department1_ on employee0_.DEPARTMENT_ID=department1_.DEPARTMENT_ID where (employee0_.EMP_NAME=? ) #Prints the Employee and Department fine Employee : [Id : 1, name : testEmployee], Employee.Department : [Id : 1, name : testDepartment] #Second read bombs! org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:84) at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134) [..] Please have a look at the post [http://forum.hibernate.org/viewtopic.php?t=955839] for more details and follow ups. Kindly help. I am attaching an Eclipse Project containing the TestCase. The main file is TestPrefetchRelationWithQueryCacheEnabled. Thanks, Vikas
    via by Vikas Sasidharan,
    • org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:80) at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134) at com.manheim.dms.entity.security.vo.Role$$EnhancerByCGLIB$$4ad14d28.getActive(<generated>) at com.manheim.dms.system.SecurityManager.buildProfile(SecurityManager.java:85) at com.manheim.dms.handler.AdminHandler.authenticate(AdminHandler.java:147) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:324) at com.cysive.framework.servlet.controller.ProxyRequestHandler.processRequest(ProxyRequestHandler.java:104) at com.cysive.framework.servlet.controller.Dispatcher.processAction(Dispatcher.java:1040) at com.cysive.framework.servlet.controller.Dispatcher.processRequest(Dispatcher.java:1081) at com.cysive.framework.servlet.controller.Dispatcher.doPost(Dispatcher.java:305) at javax.servlet.http.HttpServlet.service(HttpServlet.java:709) at javax.servlet.http.HttpServlet.service(HttpServlet.java:802) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214) at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520) at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152) at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137) at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118) at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520) at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929) at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705) at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)

    Users with the same issue

    derprogger
    1 times, last one,
    linxiaolong
    2 times, last one,
    SteVio1989
    124 times, last one,
    tomthomas
    1 times, last one,
    Unknown visitor1 times, last one,
    17 more bugmates