-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: bi-directional association
PostPosted: Fri Dec 03, 2004 5:47 am 
Beginner
Beginner

Joined: Thu Sep 02, 2004 9:17 am
Posts: 21
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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 6:35 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
You are access not initialized data outside a hibernate session scope.

Set the associations to non-lazy or make sure they are initialized before leaving the session scope.

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 7:29 am 
Newbie

Joined: Mon May 10, 2004 7:58 am
Posts: 15
since you're working on a web-app, you may also consider Spring"s OpenSessionInView support (OpenSessionInViewFilter or OpenSessionInViewInterceptor).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 7:40 am 
Beginner
Beginner

Joined: Thu Sep 02, 2004 9:17 am
Posts: 21
Thanks for the comments.

I am using Spring's open-session-in-view interceptor. But I still get the lazy-load problem.

How do I "initialise" the collection? Does that mean that I need to iterate over the set of BObjects (but not actually use them for anything) before I add a new BObject and try to save the containing AObject?

Peter


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 9:05 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
RTM chapter 6.5 of the reference manual.

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 9:08 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
BTW:
I don't know Spring. Athe point in code where you get the exception is the Session suposed to be alive? If yes there's a problem with your session handling, if no you should do the initialization as described in chapter 6.5.

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 12:09 pm 
Newbie

Joined: Mon May 10, 2004 7:58 am
Posts: 15
pak wrote:
.
How do I "initialise" the collection? Does that mean that I need to iterate over the set of BObjects (but not actually use them for anything) before I add a new BObject and try to save the containing AObject?


You don't have to explicitly initialize a collection if you keep working with your session open.
If you really want to eagerly load your lazy collection before closing the session :
Code:
Hibernate.initialize(a.getBs());


hth

[/code]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.