jQuery in Action, 3rd edition
The moose likes Spring and the fly likes spring hinbernet integration session problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "spring hinbernet integration session problem " Watch "spring hinbernet integration session problem " New topic

spring hinbernet integration session problem

yogesh jarad

Joined: Jun 10, 2010
Posts: 5
how to slove the org.hibernate.LazyInitializationException?

This is my spring-config.xml

Roberto Perillo

Joined: Dec 28, 2007
Posts: 2271

Howdy, Yogesh. Welcome to JavaRanch!

Champ, I'd kindly like to ask you to put your code between [code][/code] tags whenever you post code. I edited your post and included these tags. UseCodeTags

Now, you somewhere have a OneToMany relationship, right? Try changing its fetch mode to eager initialization and let us know if it works.

Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Actually, while you can change your mapping to eager, that is not the best way or the best approach. Eager fetching should be set at query time, never in mapping. If you put it in mapping you will get rid of any LazyInitializationException, but you will slow down your use cases that don't need all that data. You would be getting too much data from the database and have to trasnfer all that data on the wire when that particular use case might not need it.

Fetch mode should always be a query time thing.

LazyInitializationException occurs when you haven't fetched all the data you need and you close the Session and then try to access that data not fetched. Since Hibernate will have proxies for that data, then it will try to connect to the database but since there is no Session there is no Connection to the database to get that other data.

Also, this is a Hibernate question, not a Spring question so should have been posted in the ORM forum.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Sorry, I deleted your other thread about Open Session In View. While that also would work, you are still adding patches to the actual problem of not getting exactly the amount of data your use case needs at the time of query and waiting till the jsp page to do more querying.

I agree. Here's the link: http://aspose.com/file-tools
subject: spring hinbernet integration session problem
It's not a secret anymore!