Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

LazyInitializationException : Unable to retrieve values from Database sometimes .

 
Ramesh Jothimani
Ranch Hand
Posts: 70
Android Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic