-->
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.  [ 3 posts ] 
Author Message
 Post subject: many-to-many - how to "fetch join" correctly?
PostPosted: Mon Aug 13, 2007 12:45 am 
Newbie

Joined: Sat Jul 14, 2007 1:41 am
Posts: 6
I have bi-directional many-to-many relationship between two entities, let's say Department and Employee. In the database this is realized through the two main tables DEPARTMENT and EMPLOYEE and the intermediary table DEP_EMP. In the Java classes this relationship is defined through collections (Set) and is mapped as follows:

-- from Department.hbm.xml --
<set name="employees" table="DEP_EMP" cascade="save-update" fetch="join">
<key>
<column name="DEP_ID" not-null="true" />
</key>
<many-to-many entity-name="domain.Employee">
<column name="EMP_ID" not-null="true" />
</many-to-many>
</set>

-- from Employee.hbm.xml --
<set name="departments" table="DEP_EMP" inverse="true" cascade="save-update">
<key>
<column name="EMP_ID" not-null="true" />
</key>
<many-to-many entity-name="domain.Department">
<column name="DEP_ID" not-null="true" />
</many-to-many>
</set>

Now, I want to eagerly load the 'employees' collection for the Department instance without sending additional 'select' queries to the database. This is why I added fetch="join" to this collection mapping (see above).

This works perfectly if I load one Deparment instance from the database. However, if I load multiple departments like this:
List<Department> = session.createQuery("from Department").list();
- then 'employees' collections are not loaded into the Department instances, and extra selects are needed. I guess, this is what they call the 'n+1' problem.

How should I do this properly? What is the query (HQL or SQL)?

I figure I have to do createSQLQuery() including 'left join fetch'. Also, addEntity() and addJoin() should be called. I just cannot figure out details fast enough. Please help!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 13, 2007 3:00 am 
Beginner
Beginner

Joined: Sat Jan 14, 2006 10:05 am
Posts: 22
Location: spb.ru
Hi

May be it would help

Code:
List<Department> duplicatedDepts = session.createQuery("from Department d left join fetch d.employees").list();
Collection<Department> uniqResults = new LinkedHashSet<Department>(duplicatedDepts);


Are you really sure in your cascade attributes?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 13, 2007 11:05 am 
Newbie

Joined: Sat Jul 14, 2007 1:41 am
Posts: 6
This DID work. Thank you!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.