-->
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.  [ 4 posts ] 
Author Message
 Post subject: Traversing object graph and LazyInitializationException
PostPosted: Tue Mar 14, 2006 7:10 pm 
Newbie

Joined: Tue Mar 14, 2006 6:48 pm
Posts: 9
Hibernate version:3.1

In my web application I have a Manufacturer which can have many Products (a simple one-to-many relationship) and the following sequence of requests/responses:

1. Request 1 opens Session 1 and fetches Manufacturers
2. Response 1 displays Manufacturers in list (does NOT fetch related Products)
3. User selects Products disclosure button to see products for that manufacturer
4. Request 2 opens Session 2 and calls manufacturer.getProducts() which throws the LazyInitializationException

I am using Spring and OpenSessionInViewInterceptor which works great on all pages except for this one.

Everything is working fine when I do the fetch of the Manufacturers and the call to getProducts() in the same request with the same session. Also in Request 2 Session 2, I do not explicitly fetch the Manufacturer again.

Here is my question. In order for lazy initialization to work, do I need to fetch the Manufacturer in the session that I want to use the getProducts() method?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 11:19 pm 
Newbie

Joined: Tue Mar 14, 2006 5:06 pm
Posts: 4
RS, are you re-attaching the Manufacturer object to the new session before you attempt to retrieve its members?

See http://www.google.com/search?hl=en&lr=& ... te+session
for several good options.

Please leave feedback if this is helpful :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 15, 2006 1:39 am 
Newbie

Joined: Wed Mar 08, 2006 12:03 am
Posts: 4
http://www.hibernate.org/hib_docs/v3/re ... e-detached


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 15, 2006 8:53 pm 
Newbie

Joined: Tue Mar 14, 2006 6:48 pm
Posts: 9
That's exactly what happened. The object was not attached to the new session and therefore I could not traverse the relationship to Products.

Here is what I ended up doing:

In my FormController I called:

Code:
getManufacturerDao().reattachToSession(manufacturer);
manufacturer.getProducts();


In my ManufacturerDao I did:

Code:
public void reattachToSession(Tblorgbase orgbase) {
   //getSessionFactory().getCurrentSession().update(orgbase);
   getSessionFactory().getCurrentSession().lock(orgbase, org.hibernate.LockMode.NONE);
}


update or lock, both methods work.

Thanks for the help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.