-->
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.  [ 9 posts ] 
Author Message
 Post subject: parent/child load based on specific child criteria
PostPosted: Wed Oct 05, 2005 11:28 am 
Newbie

Joined: Wed Oct 05, 2005 11:17 am
Posts: 2
Consider the relationship Parent to Child.

The Parent has many children, each with different hair color.

I want to load a Parent object and its contained Child objects using the hair color as the criteria.

so... What I should end up with is a loaded Parent object and its contained Child objects where the Child object has hair color of "brown".

Another way to understand the result set I'm looking for is to read the sql below, it gives me the correct parents and children based on my criteria.

select
p.*, c.*
from
parent p,
child c
where
p.id = c.id and
c.hair_color = 'brown'

With NHibernate, I am able to load the correct parent object based on the child criteria, however, when I ask for the children collection from the parent, it returns all the children associated with the parent, when I really just want the one I asked for.

Thoughts?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 6:04 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
That's just the way it works. parent.Children always means all its children, no matter where you got the parent from.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 11:27 am 
Newbie

Joined: Wed Oct 05, 2005 11:17 am
Posts: 2
how can I load the objects I want with out going back to the database? Is there a way to do it on one shot?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 2:44 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 8:45 am
Posts: 226
If the Child object has a Parent reference, you could just return the child object(s) that match the hair color. The child would bring the parent along with it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 6:18 am 
Senior
Senior

Joined: Thu Aug 25, 2005 3:35 am
Posts: 160
i have a similar situation.. i was able to do the following (loosely translated to your situation)

"from parent p inner join fetch p.children as child where child.haircolor = "brown" "

something like that works for me..


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 2:53 am 
Newbie

Joined: Sun Oct 16, 2005 4:54 am
Posts: 7
TheShark wrote:
i have a similar situation.. i was able to do the following (loosely translated to your situation)

"from parent p inner join fetch p.children as child where child.haircolor = "brown" "

something like that works for me..


Do you mind sharing some code on how to you solve this problem?
NHibernate document is not complete therefore it is hard for newbie like to me to get speed on the tool.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 24, 2005 6:35 am 
Senior
Senior

Joined: Thu Aug 25, 2005 3:35 am
Posts: 160
I'm not quite sure what code you are looking for. the hql I posted works for me. :roll:

Tell me how I can help you.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 24, 2005 1:45 pm 
Couldn't you just add custom filter to set/bag definitions? (just like the one in table). I think this will be useful.


Top
  
 
 Post subject:
PostPosted: Wed Jan 04, 2006 12:58 am 
Newbie

Joined: Wed Oct 12, 2005 10:28 pm
Posts: 10
I have the same problem but my scenario when i try to use ICriteria.

I can solve this problem using mapping xml mapping in collection but it's not dynamically:

<bag name="SurveyAnswers" inverse="true" lazy="true" cascade="all-delete-orphan" where="bIsOptionAnswer!=1">
<key column="nSurveyQuestionId" />
<one-to-many class="Survey.Core.Domain.SurveyAnswer, Survey.Core"/>
</bag>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.