You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by "Gordon, John" <jo...@wamu.net> on 2005/02/04 22:31:22 UTC

lazy load error in Page using Hibernate & Spring

Hi, All.

I'm hoping there is an easy answer to this one:

Does anyone know the right way to have a single, open hibernate session
be available to all calling objects throughout the scope of a request
when using Spring to manage Hibernate?

I'm using Spring to manage Hibernate in a Tapestry web app I'm building.
Some of my domain objects have lists of other domain objects (e.g a
Project object has a list of Task objects).  I'm trying to use
Hibernate's Lazy load functionality to cut down on the amount of data
I'm pulling out of the database but I'm running into an error when I,
later in the same request, attempt to iterate through the List of Task
objects contained in the parent "Project" object.  The error is:

Failed to lazily initialize a collection - no session or session was
closed

I know the problem:  the Hibernate Session that was used to retrieve the
parent "Project" object has been closed and I has no idea about how to
go about retrieving the child classes.  My problem is that I've tried
several ways to implement the "thread local" pattern-attaching a single
open Session to a request-but I've been unsuccessful.  

My setup:

Spring creates a SessionFactory and passes it into a single
HibernateTemplate that is used throughout the project.  DAO classes for
the domain inherit from the HibernateDaoSupport  and execute queries
using getHibernateTemplate.selectAll().

I thought the above would work as it is a single HibernateTemplate being
used, but it must be closing it somewhere.

I've created a Transaction Manager hoping it would keep an open session
for my thread (http://www.javalobby.com/articles/thread-safe/index.jsp).
No dice.

I then tried overriding the BaseEngine's setupForRequest() and
cleanupAfterRequest() to try and inject an open session into the
request's thread.  No luck there either.

Does anyone have any idea what I can do to enable lazy loading?

Thanks,

John


Re: lazy load error in Page using Hibernate & Spring

Posted by john mcteague <jo...@gmail.com>.
I have been using the same combination as you, (Spring, Tapestry and
Hibernate) and have found the best way is to use Springs open session
in view filter, and at the end of every request, either save, call
session.lock() with a LockMode of NONE or session.update() on your
persistant objects.

The problem isnt that the Session is not attached to the ThreadLocal
(which OpenSessionInView filter takaes care of), its that your
persistant hibernate objects need to be reassociated with the
hibernate session at the start of each http request. If you dont want
to use Springs filter, at the point you manually attach the session to
the ThreadLocal, you should also reassociate your objects to the
session ( if you have cascade="true" on your hibernate mappings, you
will only need to do this at the top of your data model tree)


On Fri, 4 Feb 2005 13:31:22 -0800, Gordon, John <jo...@wamu.net> wrote:
> Hi, All.
> 
> I'm hoping there is an easy answer to this one:
> 
> Does anyone know the right way to have a single, open hibernate session
> be available to all calling objects throughout the scope of a request
> when using Spring to manage Hibernate?
> 
> I'm using Spring to manage Hibernate in a Tapestry web app I'm building.
> Some of my domain objects have lists of other domain objects (e.g a
> Project object has a list of Task objects).  I'm trying to use
> Hibernate's Lazy load functionality to cut down on the amount of data
> I'm pulling out of the database but I'm running into an error when I,
> later in the same request, attempt to iterate through the List of Task
> objects contained in the parent "Project" object.  The error is:
> 
> Failed to lazily initialize a collection - no session or session was
> closed
> 
> I know the problem:  the Hibernate Session that was used to retrieve the
> parent "Project" object has been closed and I has no idea about how to
> go about retrieving the child classes.  My problem is that I've tried
> several ways to implement the "thread local" pattern-attaching a single
> open Session to a request-but I've been unsuccessful.
> 
> My setup:
> 
> Spring creates a SessionFactory and passes it into a single
> HibernateTemplate that is used throughout the project.  DAO classes for
> the domain inherit from the HibernateDaoSupport  and execute queries
> using getHibernateTemplate.selectAll().
> 
> I thought the above would work as it is a single HibernateTemplate being
> used, but it must be closing it somewhere.
> 
> I've created a Transaction Manager hoping it would keep an open session
> for my thread (http://www.javalobby.com/articles/thread-safe/index.jsp).
> No dice.
> 
> I then tried overriding the BaseEngine's setupForRequest() and
> cleanupAfterRequest() to try and inject an open session into the
> request's thread.  No luck there either.
> 
> Does anyone have any idea what I can do to enable lazy loading?
> 
> Thanks,
> 
> John
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org