-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Very newbie question on join fetch, please help?
PostPosted: Mon Aug 09, 2010 11:32 am 
Newbie

Joined: Thu Aug 05, 2010 3:20 pm
Posts: 6
Say I execute the query below

Code:
from Parent p join fetch p.child c


And I call query.list() on it. Assume the parent has two records. Parent 1 has three child records, and Parent 2 has two. I expect to get a list back of size two containing the two parents (with their children populated). Instead I get a list of size five with the parents repeated multiple times. Parent 1 is repeated three times and Parent 2 twice, I assume because that's how many children they have.

Is this the correct behavior? I am new to Hibernate, and have not found an answer to this question. What I want is a list without repeats.


Top
 Profile  
 
 Post subject: Re: Very newbie question on join fetch, please help?
PostPosted: Mon Aug 09, 2010 4:41 pm 
Newbie

Joined: Fri May 16, 2008 3:40 pm
Posts: 13
Spektr wrote:
Is this the correct behavior?
No.

The list should indeed have only two objects. Try adding "select p" to the beginning of your HQL. If that does not work either, something is wrong with your mapping.


Top
 Profile  
 
 Post subject: Re: Very newbie question on join fetch, please help?
PostPosted: Tue Aug 10, 2010 10:21 am 
Newbie

Joined: Thu Aug 05, 2010 3:20 pm
Posts: 6
Can you spot what I've done wrong? Child table has a parent_id foreign key to the parent record.

My Parent class has this:

Code:
@OneToMany
@JoinColumn(name="parent_id")
private List<Child> child;


Top
 Profile  
 
 Post subject: Re: Very newbie question on join fetch, please help?
PostPosted: Tue Aug 10, 2010 10:28 am 
Newbie

Joined: Thu Aug 05, 2010 3:20 pm
Posts: 6
I also tried this and I'm getting the same result:

Code:
@OneToMany(mappedBy="parentId")
private List<Child> child;


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.