-->
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.  [ 2 posts ] 
Author Message
 Post subject: N+1 select with composite-id and many-to-one
PostPosted: Thu Mar 24, 2005 2:19 pm 
Newbie

Joined: Sat Oct 30, 2004 3:48 pm
Posts: 14
Hibernate version:
2.1.8

Mapping documents:
<class name="edu.uga.ais.pdia.domain.User" schema="PDIA" table="DIAB99U" mutable="false">
<id name="username" column="DIAB99U_CDMYID" type="edu.uga.ais.pdia.dao.impl.CharTrimType">
<generator class="assigned"/>
</id>
<set name="userAccess" lazy="true" inverse="true" access="field">
<key column="DIAB99D_CDUSER"/>
<one-to-many class="edu.uga.ais.pdia.domain.UserAccess"/>
</set>
</class>

<class name="edu.uga.ais.pdia.domain.UserAccess" schema="PDIA" table="DIAB99D" mutable="false">
<composite-id>
<key-property name="username" column="DIAB99D_CDUSER" type="edu.uga.ais.pdia.dao.impl.CharTrimType"/>
<key-many-to-one name="department" column="DIAB99D_NODEPT"/>
<key-property name="category" column="DIAB99D_CDCATG" type="edu.uga.ais.pdia.dao.impl.CategoryUserType"/>
</composite-id>
<property name="privilege" column="DIAB99D_CDALTR" type="edu.uga.ais.pdia.dao.impl.PrivilegeUserType"/>
</class>

<class name="edu.uga.ais.pdia.domain.Department" schema="PCFA" table="BCFA20C" mutable="false">
<id name="number" column="NODEPT">
<generator class="assigned"/>
</id>
<property name="description" column="DSDEPT" type="edu.uga.ais.pdia.dao.impl.CharTrimType"/>
</class>

Code between sessionFactory.openSession() and session.close():

private static final String FIND_USER_NOTLAZY =
"from User user" +
" inner join fetch user.userAccess access" +
" inner join fetch access.department" +
" where user.username = :username";

List users = getHibernateTemplate().findByNamedParam(FIND_USER_NOTLAZY,"username",username);


The generated SQL (show_sql=true):
Hibernate: select user0_.DIAB99U_CDMYID as DIAB99U_1_0_, useraccess1_.DIAB99D_CDUSER as DIAB99D_1_1_, useraccess1_.DIAB99D_NODEPT as DIAB99D_2_1_, useraccess1_.DIAB99D_CDCATG as DIAB99D_3_1_, department2_.NODEPT as NODEPT2_, useraccess1_.DIAB99D_CDALTR as DIAB99D_4_1_, department2_.DSDEPT as DSDEPT2_, useraccess1_.DIAB99D_CDUSER as DIAB99D_1___, useraccess1_.DIAB99D_NODEPT as DIAB99D_2___, useraccess1_.DIAB99D_CDCATG as DIAB99D_3___ from PDIA.DIAB99U user0_ inner join PDIA.DIAB99D useraccess1_ on user0_.DIAB99U_CDMYID=useraccess1_.DIAB99D_CDUSER inner join PCFA.BCFA20C department2_ on useraccess1_.DIAB99D_NODEPT=department2_.NODEPT where (user0_.DIAB99U_CDMYID=? )

Hibernate: select department0_.NODEPT as NODEPT0_, department0_.DSDEPT as DSDEPT0_ from PCFA.BCFA20C department0_ where department0_.NODEPT=?

Hibernate: select department0_.NODEPT as NODEPT0_, department0_.DSDEPT as DSDEPT0_ from PCFA.BCFA20C department0_ where department0_.NODEPT=?

My Question :)

I might be understanding this wrong, but it would seem to me that the only one query would be needed to do this. The first query returns all the data that is needed to create the graph. Am I missing something?

Is is possible to load all data eagerly with these mappings?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 26, 2005 9:28 pm 
Newbie

Joined: Sat Oct 30, 2004 3:48 pm
Posts: 14
The same problem exists with the previous mapping:

return session.createCriteria(User.class)
.add(Expression.eq("username",username))
.setFetchMode("userAccess",FetchMode.EAGER)
.setFetchMode("userAccess.department",FetchMode.EAGER)
.uniqueResult();

And with the problem exists with:

getHibernateTemplate().get(User.class,username);

And this following mapping

<class name="edu.uga.ais.pdia.domain.User" schema="PDIA" table="DIAB99U" mutable="false">
<id name="username" column="DIAB99U_CDMYID" type="edu.uga.ais.pdia.dao.impl.type.CharTrimType">
<generator class="assigned"/>
</id>
<set name="userAccess" lazy="true" inverse="true" access="field" outer-join="true">
<key column="DIAB99D_CDUSER"/>
<one-to-many class="edu.uga.ais.pdia.domain.UserAccess"/>
</set>
</class>

<class name="edu.uga.ais.pdia.domain.Department" schema="PCFA" table="BCFA20C" mutable="false" lazy="false">
<id name="number" column="NODEPT">
<generator class="assigned"/>
</id>
<property name="description" column="DSDEPT" type="edu.uga.ais.pdia.dao.impl.type.CharTrimType"/>
</class>

<class name="edu.uga.ais.pdia.domain.UserAccess" schema="PDIA" table="DIAB99D" mutable="false">
<composite-id>
<key-property name="username" column="DIAB99D_CDUSER" type="edu.uga.ais.pdia.dao.impl.type.CharTrimType"/>
<key-many-to-one name="department" column="DIAB99D_NODEPT"/>
<key-property name="category" column="DIAB99D_CDCATG" type="edu.uga.ais.pdia.dao.impl.type.CategoryUserType"/>
</composite-id>
<property name="privilege" column="DIAB99D_CDALTR" type="edu.uga.ais.pdia.dao.impl.type.PrivilegeUserType"/>
</class>

Is this impossible to do or a bug?


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

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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.