You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Jonathan Millett <jo...@millett.net> on 2005/03/01 00:11:00 UTC

Re: Tapestry + Hibernate + Lazy problems

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 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