File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Spring and the fly likes LazyInitializationException(Spring-Hibernate application) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "LazyInitializationException(Spring-Hibernate application)" Watch "LazyInitializationException(Spring-Hibernate application)" New topic

LazyInitializationException(Spring-Hibernate application)

VenkataPrasad Regula
Ranch Hand

Joined: Jun 30, 2011
Posts: 63
I have got two methods in spring-hibernate project. 1)empadd() and 2) empget(). As I execute, the firstone gets executed and for the second one it says LazyInitialization exception. This means the session is getting closed after the first method is executed. To make both the methods run in the same session, I was guided to use OpenSessionInViewInterceptor. This is not a web application. It is a 2-tier application. I am finding on google the class of OpenSessionInViewInterceptor, but I am not knowing how to use it from the client code. What all I need is the same session to be for both the methods.
VenkataPrasad Regula
Ranch Hand

Joined: Jun 30, 2011
Posts: 63
Could someone give a simple but complete sample code. Thanks in advance.
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Where is your transaction boundary. A session is "scoped" to a single transaction, single user, single use case. If you have a use case that covers more than one query, you have to make sure it is still all done in one transaction and one session. Once a transaction is committed, the session is over and done.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
I agree. Here's the link:
subject: LazyInitializationException(Spring-Hibernate application)
It's not a secret anymore!