This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.
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?
Joined: Mar 29, 2006
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).
Joined: Mar 29, 2006
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 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?
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.
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.
Joined: May 14, 2008
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.