net.sf.hibernate.LazyInitializationException: Exception initializing proxy: [com.lmichasz.test.hibernate.BImpl#1]

Hibernate JIRA | Lukasz Michonski | 1 decade ago
tip
Click on the to mark the solution that helps you, Samebug will learn from it.
As a community member, you’ll be rewarded for you help.
  1. 0

    Mapping documents: <hibernate-mapping package="com.lmichasz.test.hibernate"> <class name="AImpl" proxy="A" table="A"> <id name="ID" column="ID" type="long"> <generator class="identity"/> </id> <property name="x" column="X" length="100" type="string" /> <many-to-one name="b" class="BImpl" column="B_ID" foreign-key="A_FK1"/> <bag name="bs" inverse="true" lazy="true" > <key column="A_ID"/> <one-to-many class="BImpl"/> </bag> </class> </hibernate-mapping> <hibernate-mapping package="com.lmichasz.test.hibernate"> <class name="BImpl" proxy="B" table="B"> <id name="ID" column="ID" type="long"> <generator class="identity"/> </id> <property name="x" column="X" length="100" type="string" /> <many-to-one name="a" class="AImpl" column="A_ID" foreign-key="B_FK1"/> </class> </hibernate-mapping> Code between sessionFactory.openSession() and session.close(): Configuration c = new Configuration(); c.configure(); SessionFactory sf = c.buildSessionFactory(); Session s = sf.openSession(); Transaction t = s.beginTransaction(); A a = (A)s.get(AImpl.class,new Long(1)); Hibernate.initialize(a.getBs()); t.commit(); s.close(); B b = (B)a.getBs().get(0); b.getX(); //<- Here exception is thrown sf.close(); Full stack trace of any exception that occurs: net.sf.hibernate.LazyInitializationException: Exception initializing proxy: [com.lmichasz.test.hibernate.BImpl#1] at net.sf.hibernate.proxy.LazyInitializer.initializeWrapExceptions(LazyInitializer.java:64) at net.sf.hibernate.proxy.LazyInitializer.getImplementation(LazyInitializer.java:164) at net.sf.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:108) at net.sf.hibernate.proxy.HibernateProxy$$EnhancerByCGLIB$$4daf392.getX(<generated>) at com.lmichasz.test.hibernate.test.Test.main(Test.java:42) Caused by: net.sf.hibernate.HibernateException: Could not initialize proxy - the owning Session was closed at net.sf.hibernate.proxy.LazyInitializer.initialize(LazyInitializer.java:47) at net.sf.hibernate.proxy.LazyInitializer.initializeWrapExceptions(LazyInitializer.java:60) ... 4 more The generated SQL (show_sql=true): Hibernate: select aimpl0_.ID as ID0_, aimpl0_.X as X0_, aimpl0_.B_ID as B_ID0_ from A aimpl0_ where aimpl0_.ID=? Hibernate: select bs0_.A_ID as A_ID__, bs0_.ID as ID__, bs0_.ID as ID0_, bs0_.X as X0_, bs0_.A_ID as A_ID0_ from B bs0_ where bs0_.A_ID=? Debug level Hibernate log excerpt: 22:36:48,389 DEBUG SessionImpl:3260 - initializing collection [com.lmichasz.test.hibernate.AImpl.bs#1] 22:36:48,399 DEBUG SessionImpl:3261 - checking second-level cache 22:36:48,399 DEBUG SessionImpl:3267 - collection not cached 22:36:48,399 DEBUG BatcherImpl:200 - about to open: 0 open PreparedStatements, 0 open ResultSets 22:36:48,399 DEBUG SQL:226 - select bs0_.A_ID as A_ID__, bs0_.ID as ID__, bs0_.ID as ID0_, bs0_.X as X0_, bs0_.A_ID as A_ID0_ from B bs0_ where bs0_.A_ID=? Hibernate: select bs0_.A_ID as A_ID__, bs0_.ID as ID__, bs0_.ID as ID0_, bs0_.X as X0_, bs0_.A_ID as A_ID0_ from B bs0_ where bs0_.A_ID=? 22:36:48,409 DEBUG BatcherImpl:249 - preparing statement 22:36:48,439 DEBUG Loader:402 - result set contains (possibly empty) collection: [com.lmichasz.test.hibernate.AImpl.bs#1] 22:36:48,449 DEBUG SessionImpl:3018 - uninitialized collection: initializing 22:36:48,449 DEBUG Loader:277 - processing result set 22:36:48,449 DEBUG Loader:480 - result row: 1 22:36:48,459 DEBUG Loader:611 - Initializing object from ResultSet: 1 22:36:48,459 DEBUG Loader:680 - Hydrating entity: com.lmichasz.test.hibernate.BImpl#1 22:36:48,459 DEBUG Loader:367 - found row of collection: [com.lmichasz.test.hibernate.AImpl.bs#1] 22:36:48,469 DEBUG SessionImpl:3041 - reading row 22:36:48,469 DEBUG SessionImpl:1986 - loading [com.lmichasz.test.hibernate.BImpl#1] 22:36:48,479 DEBUG SessionImpl:2083 - attempting to resolve [com.lmichasz.test.hibernate.BImpl#1] 22:36:48,479 DEBUG SessionImpl:2099 - resolved object in session cache [com.lmichasz.test.hibernate.BImpl#1] 22:36:48,479 DEBUG Loader:294 - done processing result set (1 rows) 22:36:48,479 DEBUG BatcherImpl:207 - done closing: 0 open PreparedStatements, 0 open ResultSets 22:36:48,499 DEBUG BatcherImpl:269 - closing statement 22:36:48,499 DEBUG Loader:314 - total objects hydrated: 1 22:36:48,509 DEBUG SessionImpl:2202 - resolving associations for [com.lmichasz.test.hibernate.BImpl#1] 22:36:48,509 DEBUG SessionImpl:1986 - loading [com.lmichasz.test.hibernate.AImpl#1] 22:36:48,509 DEBUG SessionImpl:2083 - attempting to resolve [com.lmichasz.test.hibernate.AImpl#1] 22:36:48,509 DEBUG SessionImpl:2099 - resolved object in session cache [com.lmichasz.test.hibernate.AImpl#1] 22:36:48,509 DEBUG SessionImpl:2226 - done materializing entity [com.lmichasz.test.hibernate.BImpl#1] 22:36:48,519 DEBUG SessionImpl:3077 - 1 collections were found in result set 22:36:48,519 DEBUG SessionImpl:3095 - collection fully initialized: [com.lmichasz.test.hibernate.AImpl.bs#1] 22:36:48,529 DEBUG SessionImpl:3098 - 1 collections initialized 22:36:48,529 DEBUG SessionImpl:3116 - initializing non-lazy collections 22:36:48,529 DEBUG SessionImpl:3269 - collection initialized 22:36:48,529 DEBUG JDBCTransaction:59 - commit 22:36:48,539 DEBUG SessionImpl:2246 - flushing session 22:36:48,539 DEBUG SessionImpl:2439 - Flushing entities and processing referenced collections 22:36:48,589 DEBUG SessionImpl:2884 - Collection found: [com.lmichasz.test.hibernate.AImpl.bs#1], was: [com.lmichasz.test.hibernate.AImpl.bs#1] 22:36:48,589 DEBUG SessionImpl:2780 - Processing unreferenced collections 22:36:48,589 DEBUG SessionImpl:2794 - Scheduling collection removes/(re)creates/updates 22:36:48,589 DEBUG SessionImpl:2270 - Flushed: 0 insertions, 0 updates, 0 deletions to 2 objects 22:36:48,599 DEBUG SessionImpl:2275 - Flushed: 0 (re)creations, 0 updates, 0 removals to 1 collections 22:36:48,599 DEBUG Printer:75 - listing entities: 22:36:48,599 DEBUG Printer:82 - com.lmichasz.test.hibernate.BImpl{a=AImpl#1, x=null, ID=1} 22:36:48,609 DEBUG Printer:82 - com.lmichasz.test.hibernate.AImpl{b=BImpl#1, bs=[BImpl#1], x=null, ID=1} 22:36:48,609 DEBUG SessionImpl:2359 - executing flush 22:36:48,619 DEBUG SessionImpl:2824 - post flush 22:36:48,619 DEBUG SessionImpl:585 - transaction completion 22:36:48,629 DEBUG SessionImpl:573 - closing session 22:36:48,629 DEBUG SessionImpl:3336 - disconnecting session 22:36:48,639 DEBUG DriverManagerConnectionProvider:120 - returning connection to pool, pool size: 1 22:36:48,639 DEBUG SessionImpl:585 - transaction completion 22:36:48,649 ERROR LazyInitializer:63 - Exception initializing proxy

    Hibernate JIRA | 1 decade ago | Lukasz Michonski
    net.sf.hibernate.LazyInitializationException: Exception initializing proxy: [com.lmichasz.test.hibernate.BImpl#1]
  2. 0

    Mapping documents: <hibernate-mapping package="com.lmichasz.test.hibernate"> <class name="AImpl" proxy="A" table="A"> <id name="ID" column="ID" type="long"> <generator class="identity"/> </id> <property name="x" column="X" length="100" type="string" /> <many-to-one name="b" class="BImpl" column="B_ID" foreign-key="A_FK1"/> <bag name="bs" inverse="true" lazy="true" > <key column="A_ID"/> <one-to-many class="BImpl"/> </bag> </class> </hibernate-mapping> <hibernate-mapping package="com.lmichasz.test.hibernate"> <class name="BImpl" proxy="B" table="B"> <id name="ID" column="ID" type="long"> <generator class="identity"/> </id> <property name="x" column="X" length="100" type="string" /> <many-to-one name="a" class="AImpl" column="A_ID" foreign-key="B_FK1"/> </class> </hibernate-mapping> Code between sessionFactory.openSession() and session.close(): Configuration c = new Configuration(); c.configure(); SessionFactory sf = c.buildSessionFactory(); Session s = sf.openSession(); Transaction t = s.beginTransaction(); A a = (A)s.get(AImpl.class,new Long(1)); Hibernate.initialize(a.getBs()); t.commit(); s.close(); B b = (B)a.getBs().get(0); b.getX(); //<- Here exception is thrown sf.close(); Full stack trace of any exception that occurs: net.sf.hibernate.LazyInitializationException: Exception initializing proxy: [com.lmichasz.test.hibernate.BImpl#1] at net.sf.hibernate.proxy.LazyInitializer.initializeWrapExceptions(LazyInitializer.java:64) at net.sf.hibernate.proxy.LazyInitializer.getImplementation(LazyInitializer.java:164) at net.sf.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:108) at net.sf.hibernate.proxy.HibernateProxy$$EnhancerByCGLIB$$4daf392.getX(<generated>) at com.lmichasz.test.hibernate.test.Test.main(Test.java:42) Caused by: net.sf.hibernate.HibernateException: Could not initialize proxy - the owning Session was closed at net.sf.hibernate.proxy.LazyInitializer.initialize(LazyInitializer.java:47) at net.sf.hibernate.proxy.LazyInitializer.initializeWrapExceptions(LazyInitializer.java:60) ... 4 more The generated SQL (show_sql=true): Hibernate: select aimpl0_.ID as ID0_, aimpl0_.X as X0_, aimpl0_.B_ID as B_ID0_ from A aimpl0_ where aimpl0_.ID=? Hibernate: select bs0_.A_ID as A_ID__, bs0_.ID as ID__, bs0_.ID as ID0_, bs0_.X as X0_, bs0_.A_ID as A_ID0_ from B bs0_ where bs0_.A_ID=? Debug level Hibernate log excerpt: 22:36:48,389 DEBUG SessionImpl:3260 - initializing collection [com.lmichasz.test.hibernate.AImpl.bs#1] 22:36:48,399 DEBUG SessionImpl:3261 - checking second-level cache 22:36:48,399 DEBUG SessionImpl:3267 - collection not cached 22:36:48,399 DEBUG BatcherImpl:200 - about to open: 0 open PreparedStatements, 0 open ResultSets 22:36:48,399 DEBUG SQL:226 - select bs0_.A_ID as A_ID__, bs0_.ID as ID__, bs0_.ID as ID0_, bs0_.X as X0_, bs0_.A_ID as A_ID0_ from B bs0_ where bs0_.A_ID=? Hibernate: select bs0_.A_ID as A_ID__, bs0_.ID as ID__, bs0_.ID as ID0_, bs0_.X as X0_, bs0_.A_ID as A_ID0_ from B bs0_ where bs0_.A_ID=? 22:36:48,409 DEBUG BatcherImpl:249 - preparing statement 22:36:48,439 DEBUG Loader:402 - result set contains (possibly empty) collection: [com.lmichasz.test.hibernate.AImpl.bs#1] 22:36:48,449 DEBUG SessionImpl:3018 - uninitialized collection: initializing 22:36:48,449 DEBUG Loader:277 - processing result set 22:36:48,449 DEBUG Loader:480 - result row: 1 22:36:48,459 DEBUG Loader:611 - Initializing object from ResultSet: 1 22:36:48,459 DEBUG Loader:680 - Hydrating entity: com.lmichasz.test.hibernate.BImpl#1 22:36:48,459 DEBUG Loader:367 - found row of collection: [com.lmichasz.test.hibernate.AImpl.bs#1] 22:36:48,469 DEBUG SessionImpl:3041 - reading row 22:36:48,469 DEBUG SessionImpl:1986 - loading [com.lmichasz.test.hibernate.BImpl#1] 22:36:48,479 DEBUG SessionImpl:2083 - attempting to resolve [com.lmichasz.test.hibernate.BImpl#1] 22:36:48,479 DEBUG SessionImpl:2099 - resolved object in session cache [com.lmichasz.test.hibernate.BImpl#1] 22:36:48,479 DEBUG Loader:294 - done processing result set (1 rows) 22:36:48,479 DEBUG BatcherImpl:207 - done closing: 0 open PreparedStatements, 0 open ResultSets 22:36:48,499 DEBUG BatcherImpl:269 - closing statement 22:36:48,499 DEBUG Loader:314 - total objects hydrated: 1 22:36:48,509 DEBUG SessionImpl:2202 - resolving associations for [com.lmichasz.test.hibernate.BImpl#1] 22:36:48,509 DEBUG SessionImpl:1986 - loading [com.lmichasz.test.hibernate.AImpl#1] 22:36:48,509 DEBUG SessionImpl:2083 - attempting to resolve [com.lmichasz.test.hibernate.AImpl#1] 22:36:48,509 DEBUG SessionImpl:2099 - resolved object in session cache [com.lmichasz.test.hibernate.AImpl#1] 22:36:48,509 DEBUG SessionImpl:2226 - done materializing entity [com.lmichasz.test.hibernate.BImpl#1] 22:36:48,519 DEBUG SessionImpl:3077 - 1 collections were found in result set 22:36:48,519 DEBUG SessionImpl:3095 - collection fully initialized: [com.lmichasz.test.hibernate.AImpl.bs#1] 22:36:48,529 DEBUG SessionImpl:3098 - 1 collections initialized 22:36:48,529 DEBUG SessionImpl:3116 - initializing non-lazy collections 22:36:48,529 DEBUG SessionImpl:3269 - collection initialized 22:36:48,529 DEBUG JDBCTransaction:59 - commit 22:36:48,539 DEBUG SessionImpl:2246 - flushing session 22:36:48,539 DEBUG SessionImpl:2439 - Flushing entities and processing referenced collections 22:36:48,589 DEBUG SessionImpl:2884 - Collection found: [com.lmichasz.test.hibernate.AImpl.bs#1], was: [com.lmichasz.test.hibernate.AImpl.bs#1] 22:36:48,589 DEBUG SessionImpl:2780 - Processing unreferenced collections 22:36:48,589 DEBUG SessionImpl:2794 - Scheduling collection removes/(re)creates/updates 22:36:48,589 DEBUG SessionImpl:2270 - Flushed: 0 insertions, 0 updates, 0 deletions to 2 objects 22:36:48,599 DEBUG SessionImpl:2275 - Flushed: 0 (re)creations, 0 updates, 0 removals to 1 collections 22:36:48,599 DEBUG Printer:75 - listing entities: 22:36:48,599 DEBUG Printer:82 - com.lmichasz.test.hibernate.BImpl{a=AImpl#1, x=null, ID=1} 22:36:48,609 DEBUG Printer:82 - com.lmichasz.test.hibernate.AImpl{b=BImpl#1, bs=[BImpl#1], x=null, ID=1} 22:36:48,609 DEBUG SessionImpl:2359 - executing flush 22:36:48,619 DEBUG SessionImpl:2824 - post flush 22:36:48,619 DEBUG SessionImpl:585 - transaction completion 22:36:48,629 DEBUG SessionImpl:573 - closing session 22:36:48,629 DEBUG SessionImpl:3336 - disconnecting session 22:36:48,639 DEBUG DriverManagerConnectionProvider:120 - returning connection to pool, pool size: 1 22:36:48,639 DEBUG SessionImpl:585 - transaction completion 22:36:48,649 ERROR LazyInitializer:63 - Exception initializing proxy

    Hibernate JIRA | 1 decade ago | Lukasz Michonski
    net.sf.hibernate.LazyInitializationException: Exception initializing proxy: [com.lmichasz.test.hibernate.BImpl#1]

    Root Cause Analysis

    1. net.sf.hibernate.HibernateException

      Could not initialize proxy - the owning Session was closed

      at net.sf.hibernate.proxy.LazyInitializer.initialize()
    2. net.sf.hibernate
      HibernateProxy$$EnhancerByCGLIB$$4daf392.getX
      1. net.sf.hibernate.proxy.LazyInitializer.initialize(LazyInitializer.java:47)
      2. net.sf.hibernate.proxy.LazyInitializer.initializeWrapExceptions(LazyInitializer.java:60)
      3. net.sf.hibernate.proxy.LazyInitializer.getImplementation(LazyInitializer.java:164)
      4. net.sf.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:108)
      5. net.sf.hibernate.proxy.HibernateProxy$$EnhancerByCGLIB$$4daf392.getX(<generated>)
      5 frames
    3. com.lmichasz.test
      Test.main
      1. com.lmichasz.test.hibernate.test.Test.main(Test.java:42)
      1 frame