-->
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: Hibernate fetch join and solution for select (n+1) problem
PostPosted: Mon Jun 07, 2010 12:11 pm 
Newbie

Joined: Thu Jan 25, 2007 3:52 am
Posts: 2
Location: Mumabi
Hi,
I have written one small hibernate application for testing the fetch join.
<set name="emps" lazy="false" fetch="join" order-by="deptno"
cascade="all">
<key column="DEPTNO" />
<one-to-many class="com.dcons.Emp" />
</set>

Normally if I use this , for parent one query and for childs n queries are excuting.
Even If I put the fetch=join also still (n+1) queries are executing.

SessionFactory sessionFactory = getHibernateTemplate()
.getSessionFactory();
Session session = sessionFactory.openSession();
resultList = session.createQuery("from Dept").list();

Above code I am using.

Please help me how to implement the fetch=join.
My output should be...execute the only 1 query or 2 queries one for parent and one for child.

Intersting is If I use Criteria or loadAll() methods only one query is executing.

Help me.............


Last edited by pmrushi on Tue Jun 08, 2010 2:03 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Hibernate fetch join and solution for select (n+1) problem
PostPosted: Mon Jun 07, 2010 4:38 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Fetch settings as defined in the mapping file are normally not used by HQL queries (http://docs.jboss.org/hibernate/stable/ ... ing-custom). You'll need to specify in the query that you also want to fetch the associated entities in one go. Something like this:

Code:
session.createQuery("from Dept d left join fetch d.emps")


Top
 Profile  
 
 Post subject: Re: Hibernate fetch join and solution for select (n+1) problem
PostPosted: Sun Jun 13, 2010 10:45 pm 
Newbie

Joined: Thu Jan 25, 2007 3:52 am
Posts: 2
Location: Mumabi
Thanks for your reply.

is there any other way without writing the HQL left join fetch ??

Can this possible with the mappings?


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.