File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes How to get aroun LazyInitialization exceptions using Spring HibernateDaoSupport? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to get aroun LazyInitialization exceptions using Spring HibernateDaoSupport?" Watch "How to get aroun LazyInitialization exceptions using Spring HibernateDaoSupport?" New topic
Author

How to get aroun LazyInitialization exceptions using Spring HibernateDaoSupport?

Philippe Desrosiers
Ranch Hand

Joined: Mar 29, 2006
Posts: 138
I'm implementing my entire DAO layer in a redistributable JAR file. All the DAOs extend Spring's HibernateDaoSupport, and all entities and hbm.xml files are generated using HibernateTools from the database. The problem is, when the application using the DAO attempts to drill down into a lazy-initialized child collection (such as, say, a ChannelGroup object, with a Set of Channels), Hibernate always throws a LazyInitialization exception.

As I understand it, this is because the Entity returned by the original call to ChannelGroupDao.get() has been detached from the Spring-managed Hibernate Session, so when I try to drill down into the child collection (within the client application), Hibernate throws this at me:


Setting lazy to "false" results in a Heap Space error (since Hibernate is basically attempting to load WAY too many objects into memory).

I guess I need to know how to re-attach the CategoryGroup object returned by the Dao, to the Hibernate Session being managed by HibernateTemplate, or else somehow tell the Spring Hibernate configuration to always keep the session open? I don't want the Client application to know anything about Hibernate, or what persistence mechanism is being used behind the scenes, and supposedly, HibernateTemplate releases me from having to explicitly care about Transactions and sessions and the like, but so far I'm not impressed...

This one issue is the only thing preventing me from using hibernate. I love everything else about it, but this is killing me.
deepak adlakha
Ranch Hand

Joined: Jul 27, 2001
Posts: 325
If i remember correctly "Hibernate does not support lazy initialzation for detached object".
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

If this were just a Hibernate application I'd recommend using one of the session management patterns you'll find on their site. Not sure what to do with Spring though - does it not provide support for Open Session In View and the like? I'd be very surprised if it didn't.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Philippe Desrosiers
Ranch Hand

Joined: Mar 29, 2006
Posts: 138
deepak adlakha wrote:If i remember correctly "Hibernate does not support lazy initialzation for detached object".


Hmm... So I guess I need to "re-attach" the object somehow, without the client application knowing that it's dealing with Hibernate...

Paul Sturrock wrote:If this were just a Hibernate application I'd recommend using one of the session management patterns you'll find on their site. Not sure what to do with Spring though - does it not provide support for Open Session In View and the like? I'd be very surprised if it didn't.


Hi Paul. I'm only just starting out with Hibernate, so I don't really know what you mean by Open Session In View (although I have seen the acronym OSIV floating around out there, I guess that's what it means). do you have any more information on this pattern?
Philippe Desrosiers
Ranch Hand

Joined: Mar 29, 2006
Posts: 138
I've added the OSIV interceptor in my Spring config, but it doesn't appear to make a difference (I've probably configured it wrong).

Anyway, in case it helps with the diagnosis, here's my Spring config.xml (at least the Hibernate part).



As you can probably tell, this config is cobbled together from various sources on the internet. For example, I've set Flush mode to "AUTO" in three different places, not knowing exactly where is correct (Once in HibernateTemplate, once in SessionFactory properties, and again in the OSIV interceptor).
Philippe Desrosiers
Ranch Hand

Joined: Mar 29, 2006
Posts: 138
Well, it turns out the OSIV Interceptor need something called Spring MVC, which I'm not using. So I configured an OpenSessionInView filter instead.

Here's web.xml:

Here's my Spring Hibernate config:

The *Repository classes are basic DAO-pattern classes that extend Spring HibernateDaoSupport. Here's what I'm getting in the log file when I log into the system (filtered to show only Hibernate and Spring stuff). All of this is for a single request.

You can see that multiple sessions are being opened, even though I have an OSIV filter in place. How do I tell Spring to use a thread-bound Session Factory? Would this solve the problem?
Javid Jamae
Author
Ranch Hand

Joined: May 14, 2008
Posts: 198
Are you closing the session manually in the DAO (or anywhere in your code)? Can we see your DAO code?


Author: JBoss in Action, javidjamae.com, @javidjamae on Twitter
Philippe Desrosiers
Ranch Hand

Joined: Mar 29, 2006
Posts: 138
Well, I'm loathe to stick more code in an already very verbose posting, but sure. Largely, I have a sort of heirarchical structure of Channels. Each Channel has a collection of Categories, and each Category has a collection of Media. There's a bunch of other stuff as well, but that's the nutshell. All entities are generated from the database using ANT HibernateTool hbm2java task.

Channel entity:

Here's the ChannelRepository (the DAO class). I'm sticking mostly with the Generic Data Access Objects pattern, described here:


All entities and Daos are packaged together in a JAR, which is dropped into the /lib folder of a separate, client web application to be used. I configure Hibernate in the client application using Spring (see previous post), but I do't want to have to manage transactions in the client application.

Caveat: A lot of the HQL is just plain wrong, because I'm new to Hibernate. Until I get it working, I can't debug it.

I've pretty much given up hope at this point, but if you can shed any light, I will kiss your feet.
Javid Jamae
Author
Ranch Hand

Joined: May 14, 2008
Posts: 198
I'm not quite sure what's going on because I haven't used Spring transaction management very much, but it probably has to do with the way that you have Spring configured.

Look at the second page of this conversation. And also take a look at this. Hope that helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get aroun LazyInitialization exceptions using Spring HibernateDaoSupport?