Search code examples
hibernatecriteriahibernate-criteria

Hibernate Child Collection Limited When Using Left Join in Criteria


When using hibernate criteria just altering the join type affects the results of the child collections of the root domain class.

For instance, having class Parent have a one-to-many relationship with class Child with the following data:

Parent 
| id | Name     |
|  1 | Parent 1 |

Child
| id | parent_id | Name   |
|  1 |         1 | Child1 |
|  2 |         1 | Child2 |

Using the following hibernate criteria returns the 1 parent row, and accessing the child collection results in the two rows being returned:

session.createCriteria(Parent.class)
    .createCriteria('child', CriteriaSpecification.INNER_JOIN)
    .add( Restrictions.eq( 'name', 'Child1' ) )
    .list()

However, when changing the above code with a left join, the 1 parent row is returned, but only the matching child row is returned when accessing the child collection.

session.createCriteria(Parent.class)
    .createCriteria('child', CriteriaSpecification.LEFT_JOIN)
    .add( Restrictions.eq( 'name', 'Child1' ) )
    .list()

Why does this side-effect occur? I found a few discussions about using or avoiding this side-effect depending on your intended result, but nothing about why it is there in the first place and whether it was intended. The closest direct question is an old stale defect (http://opensource.atlassian.com/projects/hibernate/browse/HHH-3872).

  • EDIT 3/24: Fixed data *

Solution

  • This problem is described here and seems to be fixed in Hibernate 3.6

    https://hibernate.onjira.com//browse/HHH-2049