Re: Hibernate LazyInitializationException in web application Struts as MVC, Spring as IOC and Hibernate as ORM layer



Jean-Baptiste Nizet wrote:
You need to understand how Hibernate handles XXX-to-many lazy relationships. Hibernate doesn't load all the objects of the collection directly. Instead, when you invoke a method of the collection for the first time, it executes an additional database query to load all the objects of the collection. In order for this query to work, the object must be associated to an open session. I suspect that you're doing the following:
1. open a session
2. load your value object using the session
3. close the session
4. call the toString() method on the value object

Since your toString() method probably invokes all the getters it finds on the value object and certainly invoke toString() on the result, Hibernate tries to load the objects in the collection, but your session is closed. So you have two solutions:

1. make sure you don't access non-loaded objects or collections of detached objects
2. make sure the session stays open until you're done with the object, in other words, don't use detached objects.

Anyway, such an implementation of toString() will cause problems with detached hibernate objects.

Even absent the lazy-load behavior (also fixable by eager loading, BTW), that may not be the most desirable implementation of 'toString()'. Does a list of all the associated objects really belong in the 'toString()' representation? What if they have a many-to-one relationship back to the parent object and their 'toString()' tries to list it? You'd have a cycle that you can't break.

--
Lew
.



Relevant Pages

  • Re: Apples customer service sucks!
    ... have a load balancer distribute requests between them. ... break foo.php on server 2 until I've updated it as well. ... BBEdit. ... balancer supports session affinity or that you're storing all ...
    (comp.sys.mac.advocacy)
  • Re: Using Perl to find what address bar says
    ... I'm hoping there's a way to do it in Perl. ... Now, load this normally in your browser, then load it again using ... Doing a side-by-side comparison, you'll see several discrepancies. ... so, I can simply load a session variable on one page, then ...
    (comp.lang.perl.misc)
  • Re: Dynamically adding in User Controls
    ... This would allow me to load or not load depending on values in my session ... that the UserControl is part of the ... Control headerControl = Page.LoadControl; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Page load frequency
    ... Erwin Moller wrote: ... but if the load of running the whole page is much higher, ... first question: Session. ... delete from tblrequest ALL requests older than ...
    (comp.lang.php)
  • Re: Hibernate probs
    ... The Taskbar icons are weird and can't find the ... Can't load IE. ... > fast log on by hibernate. ... MVP Windows - Security ...
    (microsoft.public.security.virus)