Friday, March 13, 2009

Duplicated resultsets by join fetch

Hibernate's join fetch is supposed to reduce resultset returned. But it returns duplicated results and behavior is not consistent between specifying join fetch in HQL and mapping.

For example, Department (Dept) has many employees (Emp) and sales deparment has 5 employees:
  • in HQL: from Dept d join fetch d.emps where d.name = 'sales' will return 5 exact same department records, each with totally initialized employees
  • in mapping: <set name="emps" table="EMP" inverse="true" fetch="join"> will only return 1 department with 5 initialized employees. This works as expected.
Hibernate has some suggestions: http://www.hibernate.org/117.html#A12

One possible workaround is using setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY). This works, although I feel they should fix it in the core.

Also see: http://glueclue.blogspot.com/2006/02/hibernate-duplicates-with-join-fetch.html

No comments:

Post a Comment