aspose file tools*
The moose likes Spring and the fly likes LazyInitializationException [spring + struts2] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "LazyInitializationException [spring + struts2]" Watch "LazyInitializationException [spring + struts2]" New topic
Author

LazyInitializationException [spring + struts2]

Oron Subayi
Greenhorn

Joined: Aug 22, 2011
Posts: 9
Hello
I don't know what to do : )
I have a db with data, and I wanted to get a Branch object, but I get LazyInitializationException, because the object has references to other objects inside.
I also have a class named AbstractDAO, which all specific DAOs extend, and I want it to work with HibernateTemplate, but I can't make spring inject the SessionFactory object.

Please help : /
Here are the files:


Branch:


AbstractDAO (I want to change this class):


web.xml:


applicationContext:


BranchDAOHibernate:


struts.xml


BranchById (Struts Action):
Pankaj Kumarkk
Ranch Hand

Joined: Apr 17, 2011
Posts: 108
I think you will have to ask a specific question like which method call is making the exception. Also the trace of exception would tell you the object for which you are getting this exception.
Typically you get this exception when you are accessing data from a hibernate pojo after the hibernate session has been closed. In your scenario the hibernate pojo has not been initialized and thus is just a proxy and thus accessing any data member would cause this exception.
There could be 2 solution to resolve this:
1. expand the scope of Hibernate session so that it is not closed till the time you have code to access the data
2. Eagerly load the hibernate object so that you can work with it in a detached fashion.

Option 2 should be mostly preferred.
Oron Subayi
Greenhorn

Joined: Aug 22, 2011
Posts: 9
Pankaj Kumarkk wrote:I think you will have to ask a specific question like which method call is making the exception. Also the trace of exception would tell you the object for which you are getting this exception.
Typically you get this exception when you are accessing data from a hibernate pojo after the hibernate session has been closed. In your scenario the hibernate pojo has not been initialized and thus is just a proxy and thus accessing any data member would cause this exception.
There could be 2 solution to resolve this:
1. expand the scope of Hibernate session so that it is not closed till the time you have code to access the data
2. Eagerly load the hibernate object so that you can work with it in a detached fashion.

Option 2 should be mostly preferred.


It is not exactly a method
When I call find() in BranchDAOHibernate, it brings me the branch, but the value of it's inner objects contains something weird (line 101 in stack trace), and when it tries to generate a json, the exception comes (I guess LOL)

Does the filter declared the web.xml shouldn't solve the issue you've mentioned?


lines 6 and 101
ntumba lobo
Ranch Hand

Joined: Oct 21, 2008
Posts: 180
I think the problem comes from the AbstractDAO.
1) All the declared methods close the hibernate session in the finally section. There is no chance that any lazy initialization will take place after the DAO completes.
2) The DAO seems to be using a Hibernate session different to the session created by the OpenSessionInViewFilter.
To use the session from the OpenSessionInViewFilter you need to get the session with the method getCurrentSession() but I have seen in your DAO code "HibernateUtil.openSession()"
Again with that no chance lazy init will take place.

I hope that helps


SCJP 5 , SCWCD 5, SCEA 5
Oron Subayi
Greenhorn

Joined: Aug 22, 2011
Posts: 9
ntumba lobo wrote:I think the problem comes from the AbstractDAO.
1) All the declared methods close the hibernate session in the finally section. There is no chance that any lazy initialization will take place after the DAO completes.
2) The DAO seems to be using a Hibernate session different to the session created by the OpenSessionInViewFilter.
To use the session from the OpenSessionInViewFilter you need to get the session with the method getCurrentSession() but I have seen in your DAO code "HibernateUtil.openSession()"
Again with that no chance lazy init will take place.

I hope that helps



Hi
Since nobody answer, I figured it out by myself : )
Thanks anyway!
ntumba lobo
Ranch Hand

Joined: Oct 21, 2008
Posts: 180
Have you tried making changes based on my previous comment first ?
Something else went wrong ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: LazyInitializationException [spring + struts2]