You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Vinicius Caldeira Carvalho <vi...@squadra.com.br> on 2005/02/28 18:25:09 UTC

Tapestry + Hibernate + Lazy problems

Hello there! Well, being a newbie to tapestry (but an excited one, I'm 
lovin' it) I havent build a full functional app yet. We have an 
application that so far only things that are certain is Hibernate as ORM 
and we'll use Spring as conteiner (another thing we aren't very 
skillfull, we know the risks but we're willing to take it). I've been 
saying to my folks to embrace tapestry as default view framework. But 
one raises a question on a list which kind let me confused. It's about 
lazy sets on hibernate and the tapestry model way. The debate is 
happening at hibernate forums : 
http://forum.hibernate.org/viewtopic.php?p=2232195#2232195

Is lazy instantiation so hard to do on tapestry as told? sorry I haven't 
had the time to even try it, guess in this point I had a lazy object (me)


Thanks

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


Re: Tapestry + Hibernate + Lazy problems

Posted by Henri Dupre <he...@gmail.com>.
On Mon, 28 Feb 2005 20:11:00 -0300, Jonathan Millett <jo...@millett.net> wrote:
> You might want to see Richard Hensley's approach using DataSqueezer:
> http://thread.gmane.org/gmane.comp.java.tapestry.user/15398
> 
> He creates a custom DataSqueezer that tapestry will invoke automatically.
> Using this technique, a data object is squeezed into a short form (its
> id and classname) when tapestry builds a link or a hidden form field.
> When that url or form field comes back in a later request or form
> submission, tapestry invokes the datasqueezer to unsqueeze it.  The
> unsqueeze operation re loads the dataobject from the database (or cache)
> and hence it is in the current session.
> 
> I recently converted my app to this approach (I was passing around IDs
> directly before) and it is working well.
> I use hibernate too and can post samples if you're interested.

Yes I'd be very interested to see samples... Implementing the
DataSqueezer adapter is not difficult but I'm curious to see how you
pass the hibernate session to the engine class. I'm not finding it
straightforward to write something clean for it.

Henri.

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


Re: Tapestry + Hibernate + Lazy problems

Posted by Henri Dupre <he...@gmail.com>.
I'll give a try to the DataSqueezer approach... It could probably make
session loading quite transparent. I'm thinking of adding an empty
interface to all my hibernate objects to identify them in the
DataSqueezer and then reload them inside using their identifiers.

Let's open a wiki page so I can post my configurations and Jon can
post his DataSqueezer code.

Henri.

On Mon, 28 Feb 2005 20:11:00 -0300, Jonathan Millett <jo...@millett.net> wrote:
> You might want to see Richard Hensley's approach using DataSqueezer:
> http://thread.gmane.org/gmane.comp.java.tapestry.user/15398
> 
> He creates a custom DataSqueezer that tapestry will invoke automatically.
> Using this technique, a data object is squeezed into a short form (its
> id and classname) when tapestry builds a link or a hidden form field.
> When that url or form field comes back in a later request or form
> submission, tapestry invokes the datasqueezer to unsqueeze it.  The
> unsqueeze operation re loads the dataobject from the database (or cache)
> and hence it is in the current session.
> 
> I recently converted my app to this approach (I was passing around IDs
> directly before) and it is working well.
> I use hibernate too and can post samples if you're interested.
> 
> There is also a wiki article on the data squeezer:
> http://wiki.apache.org/jakarta-tapestry/DataSqueezer
> 
> Jon
> 
> Shawn Church wrote:
> 
> >I'm using Tapestry, Spring, and Hibernate, along with Spring's
> >OpenSessionInViewFilter.  There are certain cases in which I have to be
> >careful in the way I use detached objects with Tapestry (although this
> >issue is not unique to Tapestry).  There are probably better solutions
> >which I am not yet aware of, and Hibernate 3 may handle this situation
> >better, but here's an example:
> >
> >1. Create an ordinary many-to-one Cat to Kittens, lazy=true.
> >2. Load Cat in page ListCats, but don't touch Kittens.
> >3. In page ListCats, instantiate a new Page instance ViewCat.
> >4. Set the Cat property for page ViewCat (ie - viewCat.setCat(cat)).
> >5. Page ViewCat is activated and begins rendering.
> >6. Page ViewCat tries to list this Cat's Kittens, for example in a
> >Foreach as in <span jwcid="@Foreach" source="ognl:cat.kittens"
> >value="ognl:kitten">.
> >
> >Page rendering fails with a LazyInitializationException, even though the
> >OpenSessionInViewFilter opened a session, since the Cat instance is not
> >attached to the open session.
> >
> >I've gotten around this by adding an attach() method to each of my
> >service implementations.  This method invokes getSession().lock( object,
> >LockMode.NONE) to reassociate the specified object.  I then manually
> >invoke attach() to reassociate the detached objects for the given page.
> > For example, in ViewCat.pageBeginRender(), I perform a
> >getCatService().attach(getCat()).
> >
> >I don't particularly like this solution however, since the page
> >shouldn't have to worry about these lower-level session details and it
> >wouldn't necessarily make sense if I were to switch to another
> >persistence framework.  I hope to find a cleaner solution (I'm open to
> >suggestions), but this approach has worked pretty well for me.
> >
> >Shawn
> >
> >
> >---------------------------------------------------------------------
> >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: Tapestry + Hibernate + Lazy problems

Posted by Jonathan Millett <jo...@millett.net>.
You might want to see Richard Hensley's approach using DataSqueezer:
http://thread.gmane.org/gmane.comp.java.tapestry.user/15398

He creates a custom DataSqueezer that tapestry will invoke automatically.
Using this technique, a data object is squeezed into a short form (its 
id and classname) when tapestry builds a link or a hidden form field.  
When that url or form field comes back in a later request or form 
submission, tapestry invokes the datasqueezer to unsqueeze it.  The 
unsqueeze operation re loads the dataobject from the database (or cache) 
and hence it is in the current session.

I recently converted my app to this approach (I was passing around IDs 
directly before) and it is working well.
I use hibernate too and can post samples if you're interested.

There is also a wiki article on the data squeezer:
http://wiki.apache.org/jakarta-tapestry/DataSqueezer

Jon

Shawn Church wrote:

>I'm using Tapestry, Spring, and Hibernate, along with Spring's
>OpenSessionInViewFilter.  There are certain cases in which I have to be
>careful in the way I use detached objects with Tapestry (although this
>issue is not unique to Tapestry).  There are probably better solutions
>which I am not yet aware of, and Hibernate 3 may handle this situation
>better, but here's an example:
>
>1. Create an ordinary many-to-one Cat to Kittens, lazy=true.
>2. Load Cat in page ListCats, but don't touch Kittens.
>3. In page ListCats, instantiate a new Page instance ViewCat.
>4. Set the Cat property for page ViewCat (ie - viewCat.setCat(cat)).
>5. Page ViewCat is activated and begins rendering.
>6. Page ViewCat tries to list this Cat's Kittens, for example in a
>Foreach as in <span jwcid="@Foreach" source="ognl:cat.kittens"
>value="ognl:kitten">.
>
>Page rendering fails with a LazyInitializationException, even though the
>OpenSessionInViewFilter opened a session, since the Cat instance is not
>attached to the open session.
>
>I've gotten around this by adding an attach() method to each of my
>service implementations.  This method invokes getSession().lock( object,
>LockMode.NONE) to reassociate the specified object.  I then manually
>invoke attach() to reassociate the detached objects for the given page.
> For example, in ViewCat.pageBeginRender(), I perform a
>getCatService().attach(getCat()).  
>
>I don't particularly like this solution however, since the page
>shouldn't have to worry about these lower-level session details and it
>wouldn't necessarily make sense if I were to switch to another
>persistence framework.  I hope to find a cleaner solution (I'm open to
>suggestions), but this approach has worked pretty well for me.
>
>Shawn
>
>
>---------------------------------------------------------------------
>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: Tapestry + Hibernate + Lazy problems

Posted by Shawn Church <sh...@boxity.com>.
I'm using Tapestry, Spring, and Hibernate, along with Spring's
OpenSessionInViewFilter.  There are certain cases in which I have to be
careful in the way I use detached objects with Tapestry (although this
issue is not unique to Tapestry).  There are probably better solutions
which I am not yet aware of, and Hibernate 3 may handle this situation
better, but here's an example:

1. Create an ordinary many-to-one Cat to Kittens, lazy=true.
2. Load Cat in page ListCats, but don't touch Kittens.
3. In page ListCats, instantiate a new Page instance ViewCat.
4. Set the Cat property for page ViewCat (ie - viewCat.setCat(cat)).
5. Page ViewCat is activated and begins rendering.
6. Page ViewCat tries to list this Cat's Kittens, for example in a
Foreach as in <span jwcid="@Foreach" source="ognl:cat.kittens"
value="ognl:kitten">.

Page rendering fails with a LazyInitializationException, even though the
OpenSessionInViewFilter opened a session, since the Cat instance is not
attached to the open session.

I've gotten around this by adding an attach() method to each of my
service implementations.  This method invokes getSession().lock( object,
LockMode.NONE) to reassociate the specified object.  I then manually
invoke attach() to reassociate the detached objects for the given page.
 For example, in ViewCat.pageBeginRender(), I perform a
getCatService().attach(getCat()).  

I don't particularly like this solution however, since the page
shouldn't have to worry about these lower-level session details and it
wouldn't necessarily make sense if I were to switch to another
persistence framework.  I hope to find a cleaner solution (I'm open to
suggestions), but this approach has worked pretty well for me.

Shawn


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


Re: Tapestry + Hibernate + Lazy problems

Posted by Henri Dupre <he...@gmail.com>.
At first, I would definitely suggest to go to Hibernate 3. There is in
the Spring JIRA a preliminary support for hibernate 3 which is working
very well (I have not run into any issue yet).
http://opensource.atlassian.com/projects/spring/browse/SPR-300
Hibernate 3 does lazy by default and you should do as much as you can
in lazy mode.


With Spring, the way to go is to use the "OpenSessionInViewFilter"
which opens and closes hibernate sessions for you in a transparent
way. I can post all my configurations files if you want.

Lazy is very easy to do! The only trick with the lazy mode is that
from one page to another, you are going to loose the session in the
hibernate objects referenced, so you'll need to update them to
reattach them to a session in order to perform operations on them. My
strategy that far has been to do "updates" only when I run into Lazy
loading exceptions.

Also a nice thing with hibernate is the caching. I added a cache
statement to all my hibernate objects and programatically to my
queries.

On Mon, 28 Feb 2005 14:25:09 -0300, Vinicius Caldeira Carvalho
<vi...@squadra.com.br> wrote:
> Hello there! Well, being a newbie to tapestry (but an excited one, I'm
> lovin' it) I havent build a full functional app yet. We have an
> application that so far only things that are certain is Hibernate as ORM
> and we'll use Spring as conteiner (another thing we aren't very
> skillfull, we know the risks but we're willing to take it). I've been
> saying to my folks to embrace tapestry as default view framework. But
> one raises a question on a list which kind let me confused. It's about
> lazy sets on hibernate and the tapestry model way. The debate is
> happening at hibernate forums :
> http://forum.hibernate.org/viewtopic.php?p=2232195#2232195
> 
> Is lazy instantiation so hard to do on tapestry as told? sorry I haven't
> had the time to even try it, guess in this point I had a lazy object (me)

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