Hi, I am having problems with a bi-directional association and lazy-loading.
I have two objects: AObject and BObject. AObject contains a set of BObjects, and BObject contains a set of AObjects.
The web-application I am developing is using Spring, and the problem I am seeing is when I try, for example, to add a BObject to an AObject - then I get a lazy-load exception.
Hibernate version:
2.1.6
Mapping documents:
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="dk.ciber.hibertest.AbstractBase" table="BASE" >
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id>
<joined-subclass name="dk.ciber.hibertest.AObject" table="A_OBJECT">
<key column="ID" />
<property name="name" column="NAME" type="string" />
<set name="bObjects" table="A_B" lazy="true" cascade="save-update">
<key column="A_ID"/>
<many-to-many column="B_ID" class="dk.ciber.hibertest.BObject"/>
</set>
</joined-subclass>
<joined-subclass name="dk.ciber.hibertest.BObject" table="B_OBJECT">
<key column="ID" />
<property name="name" column="NAME" type="string" />
<set name="aObjects" table="A_B" lazy="true" inverse="true" cascade="save-update">
<key column="B_ID"/>
<many-to-many column="A_ID" class="dk.ciber.hibertest.AObject"/>
</set>
</joined-subclass>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
2004-12-03 10:38:35,593 ERROR [net.sf.hibernate.LazyInitializationException] - <Failed to lazily initialize a collection - no session or session was closed>
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:209)
at net.sf.hibernate.collection.PersistentCollection.write(PersistentCollection.java:84)
at net.sf.hibernate.collection.Set.add(Set.java:154)
at dk.ciber.hibertest.AObject.addBobject(AObject.java:24)
at dk.ciber.hibertest.AEditController.onSubmit(AEditController.java:31)
at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:222)
at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:237)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:121)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:45)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:485)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:322)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:284)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:257)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:245)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:199)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:184)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:164)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:206)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:833)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:732)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:619)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:688)
at java.lang.Thread.run(Thread.java:534)
Name and version of the database you are using:
Hypersonic 1.7.1
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
2004-12-03 10:38:35,578 DEBUG [dk.ciber.hibertest.AEditController] - <No errors -> processing submit>
2004-12-03 10:38:35,578 DEBUG [org.springframework.orm.hibernate.SessionFactoryUtils] - <Opening Hibernate session>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <opened session>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <loading [dk.ciber.hibertest.BObject#30]>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <attempting to resolve [dk.ciber.hibertest.BObject#30]>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <object not resolved in any cache [dk.ciber.hibertest.BObject#30]>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.persister.NormalizedEntityPersister] - <Materializing entity: [dk.ciber.hibertest.BObject#30]>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.BatcherImpl] - <about to open: 0 open PreparedStatements, 0 open ResultSets>
2004-12-03 10:38:35,578 DEBUG [org.springframework.jdbc.datasource.DriverManagerDataSource] - <Creating new JDBC connection to [jdbc:hsqldb:hsql://localhost:21112]>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.SQL] - <select bobject0_.ID as ID0_, bobject0_.NAME as NAME3_0_ from B_OBJECT bobject0_ inner join BASE bobject0__1_ on bobject0_.ID=bobject0__1_.ID where bobject0_.ID=?>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.BatcherImpl] - <preparing statement>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.type.LongType] - <binding '30' to parameter: 1>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.loader.Loader] - <processing result set>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.loader.Loader] - <result row: 30>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.loader.Loader] - <Initializing object from ResultSet: 30>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.loader.Loader] - <Hydrating entity: dk.ciber.hibertest.BObject#30>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.type.StringType] - <returning 'B 30' as column: NAME3_0_>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.loader.Loader] - <done processing result set (1 rows)>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.BatcherImpl] - <done closing: 0 open PreparedStatements, 0 open ResultSets>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.BatcherImpl] - <closing statement>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.loader.Loader] - <total objects hydrated: 1>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <resolving associations for [dk.ciber.hibertest.BObject#30]>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <creating collection wrapper:[dk.ciber.hibertest.BObject.aObjects#30]>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <done materializing entity [dk.ciber.hibertest.BObject#30]>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <initializing non-lazy collections>
2004-12-03 10:38:35,578 DEBUG [org.springframework.orm.hibernate.HibernateTemplate] - <Eagerly flushing Hibernate session>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <flushing session>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.engine.Cascades] - <processing cascades for: dk.ciber.hibertest.BObject>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.engine.Cascades] - <cascading to collection: dk.ciber.hibertest.BObject.aObjects>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.engine.Cascades] - <done processing cascades for: dk.ciber.hibertest.BObject>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <Flushing entities and processing referenced collections>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <Collection found: [dk.ciber.hibertest.BObject.aObjects#30], was: [dk.ciber.hibertest.BObject.aObjects#30]>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <Processing unreferenced collections>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <Scheduling collection removes/(re)creates/updates>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <Flushed: 0 (re)creations, 0 updates, 0 removals to 1 collections>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.Printer] - <listing entities:>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.Printer] - <dk.ciber.hibertest.BObject{aObjects=uninitialized, name=B 30, id=30}>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <executing flush>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <post flush>
2004-12-03 10:38:35,578 DEBUG [org.springframework.orm.hibernate.SessionFactoryUtils] - <Registering Hibernate session for deferred close>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <initializing collection [dk.ciber.hibertest.BObject.aObjects#30]>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <checking second-level cache>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <collection not cached>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.BatcherImpl] - <about to open: 0 open PreparedStatements, 0 open ResultSets>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.SQL] - <select aobjects0_.B_ID as B_ID__, aobjects0_.A_ID as A_ID__, aobject1_.ID as ID0_, aobject1_.NAME as NAME1_0_ from A_B aobjects0_ inner join A_OBJECT aobject1_ on aobjects0_.A_ID=aobject1_.ID left outer join BASE aobject1__1_ on aobject1_.ID=aobject1__1_.ID where aobjects0_.B_ID=?>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.BatcherImpl] - <preparing statement>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.type.LongType] - <binding '30' to parameter: 1>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.loader.Loader] - <result set contains (possibly empty) collection: [dk.ciber.hibertest.BObject.aObjects#30]>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <uninitialized collection: initializing>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.loader.Loader] - <processing result set>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.loader.Loader] - <done processing result set (0 rows)>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.BatcherImpl] - <done closing: 0 open PreparedStatements, 0 open ResultSets>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.BatcherImpl] - <closing statement>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.loader.Loader] - <total objects hydrated: 0>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <1 collections were found in result set>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <collection fully initialized: [dk.ciber.hibertest.BObject.aObjects#30]>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <1 collections initialized>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <initializing non-lazy collections>
2004-12-03 10:38:35,578 DEBUG [net.sf.hibernate.impl.SessionImpl] - <collection initialized>
2004-12-03 10:38:35,578 INFO [dk.ciber.hibertest.AObject] - <Adding dk.ciber.hibertest.BObject[id=30,name=B 30]>
2004-12-03 10:38:35,593 ERROR [net.sf.hibernate.LazyInitializationException] - <Failed to lazily initialize a collection - no session or session was closed>
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:209)
Thanks for any pointers,
Peter
|