• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

LazyInitializationException [spring + struts2]

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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):
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Oron Subayi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried making changes based on my previous comment first ?
Something else went wrong ?
 
The problems of the world fade way as you eat a piece of pie. This tiny ad has never known problems:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic