You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Luke Galea <lg...@gmmsolutions.com> on 2004/03/19 17:22:58 UTC

Hibernate Integration

HI all,
 
I've been using Tapestry with Hibernate for some time now, but I'm
trying to do something a bit different now.
 
I have a servlet filter defined to automatically open a session when
required and close it at the end of the request.. 
 
To handle "lazy" retrieval of objects I'd like to associate my "User"
object (a member of the visit) with the session on ever page render... 
 
I've tried doing this in renderResponse method of the engine but it
looks like that get's called multiple times for each render so I get
multiple associations with session (causing errors).
 
Any ideas?
 
Thanks in advance!!

Re: Hibernate Integration

Posted by John Studarus <st...@jhlconsulting.com>.
  I've been using the Monitor model for my Hibernate sessions
but I still have a concern that others should be aware (and
maybe it could be addressed in Tapestry?).  The issue deals with
Hibernate objects living past the lifespan of a Tapestry cycle.
This is of concern when you are deleting child/parent
objects from a collection (and are using cascade all-delete-ophan)
as well as using lazy initalization of collections.  These
require the same session to be used when the object is updated
in the database.  However, all the Hibernate/Tapestry frameworks
I have seen close the session at the end of the cycle.  This
leaves these orphan Hibernate objects sitting in Tapestry pages
which cause problems at a later time.  Two instances when this occurs 
are when passing parameters to a page and when a page is being 
processed after a form submit (i.e. a delete with a collection involved
which requires a session to satisfy the lazy initalization).
  The solution (hack?) I've been using is to reload the object:

    HibernateObject orphan_object ; // initalized with an old session from 
                                    // when the page was rendered

    HibernateObject new_object = session.load(orphan_object.getId(), HibernateObject)

    doWork with the new_object

  Alternatively, the id could be stored instead of the object itself
and reloaded as needed.  Or do what I believe Luke is doing and having
a long persisting Hibernate Session running (for the duration of the
persons visit to the website).  (Not so sure if that is a great idea).

  Perhaps Tapestry can do a better job of providing an API to keep
track of page uses?  Some interface in the Engine/Monitor
that keeps track of a page use so the Session can live the duration 
of a page invocation.  Right now the best we can do is the 
serviceBegin/serviceEnd but that is called for each page/asset.
We need an API in the Monitor that spans the lifespan/display of the page and the
subsequent Action/Direct/FormSubmit to the next page.  The downside
is that the Hibernate Session (and the database connection) is
kept open for the duration that the page is displayed on the users screen.
  
  Or is everyone just using the Spring Framework and loading
objects by Id all the time?

        John






  



On Mon, Mar 22, 2004 at 09:01:05AM +0100, Karel Miarka wrote:
> Hi,
> 
> I'm using Thread Local Session pattern for obtaining Hibernate sessions and
> I'm closing it in cleanupAfterRequest() method of my Engine class.
> So I think you could use setupForRequest() method of Engine to associate the
> User object with the Hibernate session.
> 
> Regards,
> Karel
> 
> ----- Original Message ----- 
> From: "Luke Galea" <lg...@gmmsolutions.com>
> To: "'Tapestry users'" <ta...@jakarta.apache.org>
> Sent: Friday, March 19, 2004 5:22 PM
> Subject: Hibernate Integration
> 
> 
> > HI all,
> >
> > I've been using Tapestry with Hibernate for some time now, but I'm
> > trying to do something a bit different now.
> >
> > I have a servlet filter defined to automatically open a session when
> > required and close it at the end of the request..
> >
> > To handle "lazy" retrieval of objects I'd like to associate my "User"
> > object (a member of the visit) with the session on ever page render...
> >
> > I've tried doing this in renderResponse method of the engine but it
> > looks like that get's called multiple times for each render so I get
> > multiple associations with session (causing errors).
> >
> > Any ideas?
> >
> > Thanks in advance!!
> >
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org

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


Re: Hibernate Integration

Posted by Programozás <pr...@hotmail.com>.
Try to use the getRequestCycle() method of your page.
Norbi

----- Original Message ----- 
From: "Luke Galea" <lg...@gmmsolutions.com>
To: "'Tapestry users'" <ta...@jakarta.apache.org>;
<ga...@torontorehab.on.ca>
Sent: Monday, March 22, 2004 2:14 PM
Subject: RE: Hibernate Integration


> I thought about that, but in setupForRequest I couldn't see a way to get
> at the visit object.. (Ie: there is no requestCycle at that point)..
>
> Or did I miss something?
>
> -----Original Message-----
> From: Karel Miarka [mailto:kajism@issa.cz]
> Sent: March 22, 2004 3:01 AM
> To: Tapestry users; galea.luke@torontorehab.on.ca
> Subject: Re: Hibernate Integration
>
>
> Hi,
>
> I'm using Thread Local Session pattern for obtaining Hibernate sessions
> and I'm closing it in cleanupAfterRequest() method of my Engine class.
> So I think you could use setupForRequest() method of Engine to associate
> the User object with the Hibernate session.
>
> Regards,
> Karel
>
> ----- Original Message ----- 
> From: "Luke Galea" <lg...@gmmsolutions.com>
> To: "'Tapestry users'" <ta...@jakarta.apache.org>
> Sent: Friday, March 19, 2004 5:22 PM
> Subject: Hibernate Integration
>
>
> > HI all,
> >
> > I've been using Tapestry with Hibernate for some time now, but I'm
> > trying to do something a bit different now.
> >
> > I have a servlet filter defined to automatically open a session when
> > required and close it at the end of the request..
> >
> > To handle "lazy" retrieval of objects I'd like to associate my "User"
> > object (a member of the visit) with the session on ever page render...
> >
> > I've tried doing this in renderResponse method of the engine but it
> > looks like that get's called multiple times for each render so I get
> > multiple associations with session (causing errors).
> >
> > Any ideas?
> >
> > Thanks in advance!!
> >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>

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


Re: Hibernate Integration

Posted by Karel Miarka <ka...@issa.cz>.
Luke,

the Engine itself has a getVisit() method, so call it from inside the
setupForRequest()...

protected void setupForRequest(RequestContext context) {
  super.setupForRequest(context);
  Visit v = (Visit)getVisit();
  // .... do what you want ...
}

Karel


----- Original Message ----- 
From: "Luke Galea" <lg...@gmmsolutions.com>
To: "'Tapestry users'" <ta...@jakarta.apache.org>;
<ga...@torontorehab.on.ca>
Sent: Monday, March 22, 2004 2:14 PM
Subject: RE: Hibernate Integration


> I thought about that, but in setupForRequest I couldn't see a way to get
> at the visit object.. (Ie: there is no requestCycle at that point)..
>
> Or did I miss something?
>
> -----Original Message-----
> From: Karel Miarka [mailto:kajism@issa.cz]
> Sent: March 22, 2004 3:01 AM
> To: Tapestry users; galea.luke@torontorehab.on.ca
> Subject: Re: Hibernate Integration
>
>
> Hi,
>
> I'm using Thread Local Session pattern for obtaining Hibernate sessions
> and I'm closing it in cleanupAfterRequest() method of my Engine class.
> So I think you could use setupForRequest() method of Engine to associate
> the User object with the Hibernate session.
>
> Regards,
> Karel
>
> ----- Original Message ----- 
> From: "Luke Galea" <lg...@gmmsolutions.com>
> To: "'Tapestry users'" <ta...@jakarta.apache.org>
> Sent: Friday, March 19, 2004 5:22 PM
> Subject: Hibernate Integration
>
>
> > HI all,
> >
> > I've been using Tapestry with Hibernate for some time now, but I'm
> > trying to do something a bit different now.
> >
> > I have a servlet filter defined to automatically open a session when
> > required and close it at the end of the request..
> >
> > To handle "lazy" retrieval of objects I'd like to associate my "User"
> > object (a member of the visit) with the session on ever page render...
> >
> > I've tried doing this in renderResponse method of the engine but it
> > looks like that get's called multiple times for each render so I get
> > multiple associations with session (causing errors).
> >
> > Any ideas?
> >
> > Thanks in advance!!
> >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
>



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


RE: Hibernate Integration

Posted by Luke Galea <lg...@gmmsolutions.com>.
I thought about that, but in setupForRequest I couldn't see a way to get
at the visit object.. (Ie: there is no requestCycle at that point)..

Or did I miss something?

-----Original Message-----
From: Karel Miarka [mailto:kajism@issa.cz] 
Sent: March 22, 2004 3:01 AM
To: Tapestry users; galea.luke@torontorehab.on.ca
Subject: Re: Hibernate Integration


Hi,

I'm using Thread Local Session pattern for obtaining Hibernate sessions
and I'm closing it in cleanupAfterRequest() method of my Engine class.
So I think you could use setupForRequest() method of Engine to associate
the User object with the Hibernate session.

Regards,
Karel

----- Original Message ----- 
From: "Luke Galea" <lg...@gmmsolutions.com>
To: "'Tapestry users'" <ta...@jakarta.apache.org>
Sent: Friday, March 19, 2004 5:22 PM
Subject: Hibernate Integration


> HI all,
>
> I've been using Tapestry with Hibernate for some time now, but I'm 
> trying to do something a bit different now.
>
> I have a servlet filter defined to automatically open a session when 
> required and close it at the end of the request..
>
> To handle "lazy" retrieval of objects I'd like to associate my "User" 
> object (a member of the visit) with the session on ever page render...
>
> I've tried doing this in renderResponse method of the engine but it 
> looks like that get's called multiple times for each render so I get 
> multiple associations with session (causing errors).
>
> Any ideas?
>
> Thanks in advance!!
>



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



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


Re: Hibernate Integration

Posted by Karel Miarka <ka...@issa.cz>.
Hi,

I'm using Thread Local Session pattern for obtaining Hibernate sessions and
I'm closing it in cleanupAfterRequest() method of my Engine class.
So I think you could use setupForRequest() method of Engine to associate the
User object with the Hibernate session.

Regards,
Karel

----- Original Message ----- 
From: "Luke Galea" <lg...@gmmsolutions.com>
To: "'Tapestry users'" <ta...@jakarta.apache.org>
Sent: Friday, March 19, 2004 5:22 PM
Subject: Hibernate Integration


> HI all,
>
> I've been using Tapestry with Hibernate for some time now, but I'm
> trying to do something a bit different now.
>
> I have a servlet filter defined to automatically open a session when
> required and close it at the end of the request..
>
> To handle "lazy" retrieval of objects I'd like to associate my "User"
> object (a member of the visit) with the session on ever page render...
>
> I've tried doing this in renderResponse method of the engine but it
> looks like that get's called multiple times for each render so I get
> multiple associations with session (causing errors).
>
> Any ideas?
>
> Thanks in advance!!
>



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


RE: Hibernate Integration

Posted by Luke Galea <lg...@gmmsolutions.com>.
Thanks for the suggestion.. I'd seen mention of it but never got around
to researching it.

Will do!

-----Original Message-----
From: Niklas Ekman [mailto:niklas.ekman@citat.se] 
Sent: March 22, 2004 4:18 AM
To: 'Tapestry users'; galea.luke@torontorehab.on.ca
Subject: SV: Hibernate Integration


Hello

I'd really recommend using the Spring framework here. I've been using it
for a while and it absolutelly rocks with Tapestry + Hibernate + Spring.
To be able to support the thing you're after I use the
OpenSessionInViewFilter servlet filter. It works like a charm and both
lazy loading and transactions is handled like magic.

Regards,

Niklas 

-----Ursprungligt meddelande-----
Från: Luke Galea [mailto:lgalea@gmmsolutions.com] 
Skickat: den 19 mars 2004 17:23
Till: 'Tapestry users'
Ämne: Hibernate Integration


HI all,
 
I've been using Tapestry with Hibernate for some time now, but I'm
trying to do something a bit different now.
 
I have a servlet filter defined to automatically open a session when
required and close it at the end of the request.. 
 
To handle "lazy" retrieval of objects I'd like to associate my "User"
object (a member of the visit) with the session on ever page render... 
 
I've tried doing this in renderResponse method of the engine but it
looks like that get's called multiple times for each render so I get
multiple associations with session (causing errors).
 
Any ideas?
 
Thanks in advance!!


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



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


SV: Hibernate Integration

Posted by Niklas Ekman <ni...@citat.se>.
Hello

I'd really recommend using the Spring framework here. I've been using it for
a while and it absolutelly rocks with Tapestry + Hibernate + Spring. To be
able to support the thing you're after I use the OpenSessionInViewFilter
servlet filter. It works like a charm and both lazy loading and transactions
is handled like magic.

Regards,

Niklas 

-----Ursprungligt meddelande-----
Från: Luke Galea [mailto:lgalea@gmmsolutions.com] 
Skickat: den 19 mars 2004 17:23
Till: 'Tapestry users'
Ämne: Hibernate Integration


HI all,
 
I've been using Tapestry with Hibernate for some time now, but I'm trying to
do something a bit different now.
 
I have a servlet filter defined to automatically open a session when
required and close it at the end of the request.. 
 
To handle "lazy" retrieval of objects I'd like to associate my "User" object
(a member of the visit) with the session on ever page render... 
 
I've tried doing this in renderResponse method of the engine but it looks
like that get's called multiple times for each render so I get multiple
associations with session (causing errors).
 
Any ideas?
 
Thanks in advance!!


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