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 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?
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).
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?
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.