[hibernate-issues] [Hibernate-JIRA] Created: (HHH-4865) criteria on association w/ fetch=join, get n+1 queries

Kevin Howard (JIRA) noreply at atlassian.com
Fri Jan 29 13:39:29 EST 2010


criteria on association w/ fetch=join, get n+1 queries
------------------------------------------------------

                 Key: HHH-4865
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-4865
             Project: Hibernate Core
          Issue Type: Bug
          Components: query-criteria
    Affects Versions: 3.2.7
         Environment: Hibernate 3.2.7.ga
            Reporter: Kevin Howard
            Priority: Minor


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. This seems like a bug as all the data is returned in the initial query and the following queries are not needed.  I posted this in the forum (https://forum.hibernate.org/viewtopic.php?f=1&t=1002270) with no response.

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

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list