Hi,
We have a performance issue we're trying to resolve.
(All names changed to protect the innocent).
Our data model is quite simple:
Class A maps to table A
Class B maps to table B
Table B has a foreign key pointing to table A.
Class A has a list of B's.
In simplified code form:
Code:
class A {
List bs; // plus setters and getters for this and other properties.
}
Mapping fragment from A:
Code:
<list name="bs" lazy="false" fetch="join">
<key column="A_ID" not-null="true" update="false"/>
<list-index column="A_ORDER"/>
<one-to-many class="B" />
</list>
Mapping fragment from B:
Code:
<many-to-one name="a" column="A_ID" not-null="true" insert="false" update="false"/>
Now for the problem we face:
The following HQL query generates one SQL query as expected and desired:
Code:
from A a join fetch a.bs
However when I do the following Criteria query, I get n+1 SQL queries.
Code:
session.createCriteria(A.class).setFetchMode("bs", FetchMode.JOIN).createCriteria("bs").list();
However if I remove the
Code:
createCriteria("bs")
then it goes back to a single query.
This is the simplest I can describe our issue.
The obvious workaround is to rewrite our code to use HQL, but the code we have is quite a large involved user driven search, and hence why we're using the Criteria interface.
Is this a problem with the Criteria queries or is there something I am doing incorrectly?
I am using hibernate v3.1.3.