-->
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.  [ 1 post ] 
Author Message
 Post subject: Hibernate n+1 self tuning ?
PostPosted: Thu Jan 28, 2010 12:11 pm 
Newbie

Joined: Tue Jun 09, 2009 4:49 am
Posts: 5
Hi all !
I have an application where several tables are in 1-n relation.
For example:
Department 1------n Employee

Since the list() of Employees is loaded each time a Department is browsed I wanted to do some optimization to avoid the n+1 select problem

To my surprise, when I issue this code to browse the list of Employees....

Code:
Query query = hsession.createQuery("from Department ");

List <Department>list = query.list();
      
// Showing just the first department for simplicity
Department dep = list.get(0);
Set employees = dep.getEmployees();

Iterator iter = employees.iterator();
   while (iter.hasNext()) {
                 Employee emp = (Employee) iter.next();   
                 System.out.println(">>>>Name is "+emp.getEmployeeName());
   }


....I can see in the logs just one single select to retrieve all employees:

    DEBUG [org.hibernate.SQL] select employees0_.employee_department_id as employee2_1_, employees0_.employee_id as employee1_1_, employees0_.employee_id as employee1_101_0_, employees0_.employee_department_id as employee2_101_0_, employees0_.employee_name as employee3_101_0_, employees0_.employee_salary as employee4_101_0_ from hibernate.employee employees0_ where employees0_.employee_department_id=?

    DEBUG [org.hibernate.jdbc.AbstractBatcher] (http-127.0.0.1-8080-1) about to open ResultSet (open ResultSets: 0, globally: 0)

What does this mean ? Hibernate automatically batches the list of employees in a single select ?
By the way, I'm using Hibernate 3.3.1 on JBoss AS 5.0.0
Hope somebody can shed some light on it.
Thanks
John


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

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.