wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes LazyInitializationException : Unable to retrieve values from Database sometimes . Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "LazyInitializationException : Unable to retrieve values from Database sometimes ." Watch "LazyInitializationException : Unable to retrieve values from Database sometimes ." New topic
Author

LazyInitializationException : Unable to retrieve values from Database sometimes .

Ramesh Jothimani
Ranch Hand

Joined: May 05, 2011
Posts: 70


I am trying to access the values in Beneficiary_Details table using the following function (Hibernate) .




The values of the table are getting retrieved when I keep breakpoints and make some delay to execute but only NULL values are getting retrieved when I execute it all of sudden.

I am getting error in Console as :

org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed



Please help me to solve this issue !
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

Well you code looks a bit more complicated than it needs to be. I am also not sure why you are using Vectors and Arrays.

To your question though.

You will get this exception if you try to access unfetched data outside of a session context.

A few possible solutions:

1. You can either make sure you only access this when the session is still open
2. Trigger a lazy load before while the entity is still attached (i.e. by accessing the lazily loaded propertes)
3. Change your Fetch strategy to Eager
4. Change your HQL to eagerly fetch (look at the fetch keyword)

See Fetching strategies


[How To Ask Questions][Read before you PM me]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: LazyInitializationException : Unable to retrieve values from Database sometimes .