Re: Hibernate LazyInitializationException in web application Struts as MVC, Spring as IOC and Hibernate as ORM layer
- From: Lew <noone@xxxxxxxxxxxxx>
- Date: Fri, 24 Apr 2009 19:58:08 -0400
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
.
- Follow-Ups:
- References:
- Hibernate LazyInitializationException in web application Struts as MVC, Spring as IOC and Hibernate as ORM layer
- From: Amit Jain
- Re: Hibernate LazyInitializationException in web application Struts as MVC, Spring as IOC and Hibernate as ORM layer
- From: Perfect.C
- Re: Hibernate LazyInitializationException in web application Struts as MVC, Spring as IOC and Hibernate as ORM layer
- From: Amit Jain
- Re: Hibernate LazyInitializationException in web application Struts as MVC, Spring as IOC and Hibernate as ORM layer
- From: Jean-Baptiste Nizet
- Hibernate LazyInitializationException in web application Struts as MVC, Spring as IOC and Hibernate as ORM layer
- Prev by Date: Re: I thought Flash/Flex could tie into databases, so wouldn't Oracle
- Next by Date: Re: Help with Array
- Previous by thread: Re: Hibernate LazyInitializationException in web application Struts as MVC, Spring as IOC and Hibernate as ORM layer
- Next by thread: Re: Hibernate LazyInitializationException in web application Struts as MVC, Spring as IOC and Hibernate as ORM layer
- Index(es):
Relevant Pages
|