Two Laptop Bag
The moose likes Object Relational Mapping and the fly likes Getting mulltiple parent records while eager fetching child Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "Getting mulltiple parent records while eager fetching child" Watch "Getting mulltiple parent records while eager fetching child" New topic
Author

Getting mulltiple parent records while eager fetching child

Salil Surendran
Greenhorn

Joined: Jan 16, 2004
Posts: 29
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?


Thanks,<br />Salil Surendran
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 16624

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Salil Surendran
Greenhorn

Joined: Jan 16, 2004
Posts: 29
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.
 
 
subject: Getting mulltiple parent records while eager fetching child
 
Threads others viewed
How to select child object without his parent
How does Hibernate get child objects
Parent and subcollection fetching
ORM as compared to JDBC
Nested Children Access
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com