Hi,
I have a little problem with Hibernate 2.1.3.key-many-to-one.
Here is a part of my configuration, speaking for itself :
Code:
<class name="Application" table="APPLICATIONS">
<id name="id" column="APP_ID" type="java.lang.Integer" >
<generator class="native" />
</id>
...
</class>
<class name="Selection" table="SELECTIONS">
<composite-id>
<key-property
name="user"
column="SEL_USER"
type="java.lang.String" />
<key-many-to-one
name="application"
column="APP_ID"
class="Application" />
</key-many-to-one>
</composite-id>
...
</class>
Then, I want to retrieve all the 'selection' entities for a specific user,
and
fetch all the selection.application entities :
Code:
Query q = hS.createQuery("from Selection as selection " +
"left join fetch selection.application " +
"where selection.user = :user " +
"order by selection.column, selection.row")
.setString("user", username);
In the logs, I always see :
- One first SQL query to retrieve the selections
- n more SQL queries, retrieving the application for each selection.
I would like to retrieve all the selections and selection.applications in one single query - I'm sure it's possible.
So I tried to debug, and found these lines in net.sf.hibernate.loader.Loader (line 341) :
Code:
// if we know there is exactly 1 row, we can skip.
// it would be great if we could _always_ skip this;
// it is a problem for <key-many-to-one>
if ( isSingleRowLoader() && id!=null ) {
resultId = id;
}
else {
Type idType = persister.getIdentifierType();
resultId = (Serializable) idType.nullSafeGet(rs, suffixedKeyColumns[i], session, null); //problematic for <key-many-to-one>!
if ( id!=null && resultId!=null && id.equals(resultId) ) resultId = id; //use the id passed in
}
I'm not quite sure that this is about the problem I have,
but if anybody has any hint, or any piece of advice,
it would be much appreciated.
Thanks !