-->
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.  [ 3 posts ] 
Author Message
 Post subject: Reattach to Session for Lazy Loading. Ajax.
PostPosted: Fri Apr 20, 2007 8:00 am 
Newbie

Joined: Tue Aug 08, 2006 3:53 am
Posts: 17
Location: Tanznaia
Dear all,

I can reattach an individual object to a new session (with Lock or saveOrUpdate or merge) but that doesn't help my lazy-loaded sub collections. I get the dreaded LazyInitializationException.

I know this topic has had lots of discussion but I have yet to find a satisfactory answer. I am already using the OpenSessionInView pattern, and this is solving half of my problems (it's a web-app environment. ZK actually) but I'm still having situations where an object persists between two web-requests (and so between two hibernate sessions) and so I need to 'refresh' and 'reattach' the objects reliably.

It's an AJAX web-app so there are lots of quicky satisfied requests (rather than the fewer, more slowly processed requests you'd find in a traditional webapp) and most of the objects I'm using need to be retained between requests. Many simple requests translates to many hibernate sessions, with most of the objects persisting between several database sessions.

Here's an example:

In one session I fetch an object (company) from the database and display it to the user. This object contains a list of subobjects (employees) that the user chooses to look at. He/she clicks on a button and requests that the list is expanded. Back on the server, we're now in a new session so the parent object is re-attached (saveOrUpdate) and then we try to loop through the list of subitems - only to get the LazyInitializationException. Here's some psudocode...

Code:
displayEmployees(Company company) {
   Session ses = openNewSession();

   ses.saveOrUpdate(company);

   for (Employee emp : company.getEmployees()) {
      displayEmployee(emp);   //Throws Error!
   }
}


Now, I want to use lazy loading here because the sub list could be quite huge and will only be requested some of the time. I also don't want to leave the session hanging around between requests (while control is with the user) because we may be serving a hundred other users simultaneously and all these 'expensive' session objects are going to clog up the works. I understand long-lived sessions objects are frowned upon.

So, either i need to (some how) automatically cascade the ReAttaching process to subobjects and collections (is there a feature of Hibernate I'm missing here?), or manually re-attach collections before using them. Any clues?

Many thanks for your help,

Regards,

Ben.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2007 8:12 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
If the detached instance is unmodified, why not use lock() instead of saveOrUpdate(). In this circumstance, if the user is just browsing, use lock(). The saveOrUpdate() on the detached instance is obviously not reattaching it to the session, or the LIE would not occur. Try lock() and see what happens.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 22, 2007 9:42 am 
Newbie

Joined: Tue Aug 08, 2006 3:53 am
Posts: 17
Location: Tanznaia
Ananasi,

You are right, Lock does seem to work better, but not perfectly. The idea is to use lock to reattach the pojo (and it's collections) to the new session, and then call merge to make sure we've got the most up-to-date data. However, I'm getting an error, somthing like 'Object has been evicted' (I'm not sure of the exact wording - I am at a different machine just now)

Any thoughts on this one?

Cheers,

Ben


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