Getting mulltiple parent records while eager fetching child
Salil Surendran
Greenhorn
Joined: Jan 16, 2004
Posts: 29
posted
0
Hello, I have class Parent that has a one to many relationship with child. I have say a 1000 rows in my Parent table. And each of them has 3 children. I do a eager fetch of child using Criteria.
This method returns me 3000 Parent objects. I expect only 1000 but it returns me a parent object for each child.
The book 'Java Persistence with HIbernate' says that hibernate should remove duplicate records. So I expect it to be smart enought to return me 1000 Parent objects each linked to 3 children.
The same is the result with HQL too. Any solutions or is this the default behaviour in hibernate?
nope this is the default way hibernate works. One needs to use the Criteria.DISTINCT_ROOT_ENTITY AS A ResultTransformer in order to get distinct parents.