You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by George Christman <gc...@cardaddy.com> on 2012/04/27 22:13:39 UTC

Possible n+1 hibernate bug with tapestry crud.

Hello everyone, I'm experiencing some odd behavior with my form crud. I'm
using tapestry-hibernate jpa which consist of a parent object and a bunch of
children objects. I'm using the following code to get my parent object. 

@Property
private PurchaseRequest pr;

@Property
private PrLogging prLogging;

    Class<?> onActivate(PurchaseRequest pr) {
        if (pr!= null) {
            roleManager = new RoleManager(pr, this.userInfo,
this.securityService);

            if (this.roleManager.hasPrAccess()) {
                this.pr = pr;
                return null;
            }
        }        
        
        return Index.class;
    }


Object onSuccess() {
        this.pr.getPrLoggings().add(new PrLogging(this.pr,
userInfo.getUser(), _action));
        session.saveOrUpdate(pr);
        return null;
}
My .tml has a code chunk that looks something like this. 

                <t:Loop value="prLogging" source="pr.prLoggings">
                    <div>${prLogging.label}</div>
                </t:Loop>

When my page renders for the first time it will grab all my pr. prLoggings
in a single query, however when I save the form, prior to onSuccess in
onActivate the form will initiate individual queries for each and every
child result, example will initiate 10 individual prLogging queries from the
pr before even getting to the onSuccess method. 

This only happens on save and not on page refresh, does anybody know what
might be going on. Am I using tapestry incorrectly or is this a hibernate
issue. I tried eager fetching which resolved the issue within the form, but
caused other performance issues within the list page containing all the prs. 

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Possible-n-1-hibernate-bug-with-tapestry-crud-tp5671389p5671389.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: Possible n+1 hibernate bug with tapestry crud.

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Sat, 28 Apr 2012 12:59:05 -0300, George Christman  
<gc...@cardaddy.com> wrote:

> So if I needed to check to see if the user has rights to the current  
> object, how would you recommend doing this. I agree onactivate isnt the  
> spot for doing the query, but is the only spot to do the redirection.  
> Role access isnt static either. Thanks for the help guys.

In this case, you should do these checks in onActivate(). As I said in  
another thread some weeks ago, as long as you know what you're doing (i.e.  
not fetching data that isn't needed at that time), you don't need to avoid  
doing database queries in onActivate().

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

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


Re: Possible n+1 hibernate bug with tapestry crud.

Posted by George Christman <gc...@cardaddy.com>.
So if I needed to check to see if the user has rights to the current object, how would you recommend doing this. I agree onactivate isnt the spot for doing the query, but is the only spot to do the redirection. Role access isnt static either. Thanks for the help guys.
-George

----- Reply message -----
From: "Thiago H de Paula Figueiredo [via Tapestry]" <ml...@n5.nabble.com>
Date: Sat, Apr 28, 2012 11:47 am
Subject: Possible n+1 hibernate bug with tapestry crud.
To: "George Christman" <gc...@cardaddy.com>



--
View this message in context: http://tapestry.1045711.n5.nabble.com/Possible-n-1-hibernate-bug-with-tapestry-crud-tp5671389p5672800.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

Re: Possible n+1 hibernate bug with tapestry crud.

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Sat, 28 Apr 2012 08:37:31 -0300, George Christman  
<gc...@cardaddy.com> wrote:

> Hi lance, I was only doing it there inorder to handle page redirection  
> if the user didn't have role access. Is there a better way to handle the  
> redirection outside onactivate then?

onActivate() is *the* place to do redirections. Lance only said that you  
should avoid doing database queries in onActivate(), as it is invoked in  
both page render and event requests, and most of the time you probably  
don't need that database query in event requests.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

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


Re: Possible n+1 hibernate bug with tapestry crud.

Posted by George Christman <gc...@cardaddy.com>.
Hi lance, I was only doing it there inorder to handle page redirection if the user didn't have role access. Is there a better way to handle the redirection outside onactivate then?

-George

----- Reply message -----
From: "Lance Java [via Tapestry]" <ml...@n5.nabble.com>
Date: Sat, Apr 28, 2012 3:47 am
Subject: Possible n+1 hibernate bug with tapestry crud.
To: "George Christman" <gc...@cardaddy.com>



--
View this message in context: http://tapestry.1045711.n5.nabble.com/Possible-n-1-hibernate-bug-with-tapestry-crud-tp5671389p5672488.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

Re: Possible n+1 hibernate bug with tapestry crud.

Posted by Lance Java <la...@googlemail.com>.
Geoff describes it here
http://jumpstart.doublenegative.com.au/jumpstart/examples/navigation/onactivateandonpassivate/3
Basically you shouldn't do db lookups in onActivate() and should use
onPrepare() instead.
For this same reason, I think it's a bad idea to use @PageActivationContext
with a hibernate entity

Re: Possible n+1 hibernate bug with tapestry crud.

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Fri, 27 Apr 2012 17:33:01 -0300, George Christman  
<gc...@cardaddy.com> wrote:

> Hi Thiago, I'm confused, my onActivate method is called on both page  
> load and submit, both which call the same query. Does tapestry disregard  
> the object after the page's initial render requiring a second query to  
> rebuild the
> object on submit?

Yes. Redirect-after-post. Events (form submissions, component events) are  
always handled in a different request from full page rendering.

> Why would hibernate query things differently on submit
> compared to on page load. Thanks for your help.

It depends on which properties of the object you've read during the  
request. During submit, most probably you don't read lazy-loaded  
properties, but probably while rendering.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

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


Re: Possible n+1 hibernate bug with tapestry crud.

Posted by George Christman <gc...@cardaddy.com>.
Hi Thiago, I'm confused, my onActivate method is called on both page load and
submit, both which call the same query. Does tapestry disregard the object
after the page's initial render requiring a second query to rebuild the
object on submit? Why would hibernate query things differently on submit
compared to on page load. Thanks for your help. 

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Possible-n-1-hibernate-bug-with-tapestry-crud-tp5671389p5671434.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: Possible n+1 hibernate bug with tapestry crud.

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Fri, 27 Apr 2012 17:13:39 -0300, George Christman  
<gc...@cardaddy.com> wrote:

> might be going on. Am I using tapestry incorrectly or is this a hibernate
> issue.

100% Hibernate issue. Tapestry-Hibernate just provides Sessions and  
handles @CommitAfter. Anything else is up to you.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

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