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