• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting mulltiple parent records while eager fetching child  RSS feed

 
Salil Surendran
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

getSession.createCriteria(Parent.class).setFetchMode("child",FetchMode.JOIN).list();

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?
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post your mapping, it has to be something there. Maybe you don't have a n inverse set so that Hibernate knows it is bi-directional.

Mark
 
Salil Surendran
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

getSession().createCriteria(Parent.class).setFetchMode("child",FetchMode.JOIN).
setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();

will return me what i need.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!