-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: criteria on association w/ fetch=join, get n+1 queries
PostPosted: Tue Jan 26, 2010 1:18 pm 
Newbie

Joined: Tue Jan 26, 2010 10:39 am
Posts: 1
When I specify a restriction on an one-to-many association in a criteria query, Hibernate executes n+1 queries even though the first query is joining the two tables correctly with an inner join. This happens if I use createAlias or createCriteria to specify the restriction on the association. If I don't specify a restriction on the association then I do not get n+1 queries and the association is populated. Is there a way to avoid the n+1 queries when setting restrictions on associations?

Criteria Query:
final DetachedCriteria criteria = DetachedCriteria.forClass(A.class);
criteria.add(Restrictions.eq("column1", value1))
.createAlias("someSetofB", "h")
.add(Restrictions.in("h.code", new String[] {"A","B","C"}));
final List<A> list = getHibernateTemplate().findByCriteria(criteria);

Association Mapping:
<set name="someSetofB" lazy="false" fetch="join">
<key>
<column name="SOME_ID" />
</key>
<one-to-many class="B" />
</set>

SQL:
select ..(every column from A and B).. from A this_ inner join B h1_ on this_.SOME_ID=h1_.SOME_ID where this_.COLUMN_ONE=? and h1_.CODE in (?, ?, ?)
select .... from B someSetOfB0_ where someSetOfB0_.SOME_ID=?
select .... from B someSetOfB0_ where someSetOfB0_.SOME_ID=?
..... selects for as many A's returned by first query


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.