-->
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.  [ 3 posts ] 
Author Message
 Post subject: pb query parent/child where clause condition
PostPosted: Wed Jan 25, 2006 5:13 am 
Newbie

Joined: Tue Feb 22, 2005 5:27 am
Posts: 10
Hi,

I would like to do something like this, written in the FAQ :

Quote:
How do a write a query that returns objects based upon a WHERE clause condition applied to their collection elements?

There are two possibilities:
Code:
select distinct parent
from Parent parent
join parent.children child
where child.name = :name

from Parent parent where :childId in elements(parent.children)


The first query uses a table join; the second uses a subquery. The first query allows constraints to be applied to element properties; the second does not.

You may not need the distinct keyword in the first query.


It's ok, if a parent has 3 childs and one's name is :name, i'll get the parents but when I do

Code:
Parent parent = (Parent)query.list();
List listChilds = parent.getChild() ;


I have the 3 childs in listChilds, altough I expecteed to have only the Child named :name.

Somebody knows if there's a way in the mapping or in the hql query to write something to have where condition upon child and having the parent with only the childs that match the where condition.

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 25, 2006 11:32 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
This is correct behaviour. When you do a parent.getChildren it returns the children of the parent. There are 3 and thus you will get 3. Just because you used a restriction to find the parent, doesn't mean that it doesn't have 3 children any more. Thus your logic is to find the parent that has a child in with that name - and then ask the parent for all it's children.

What you could do is to add a method in your childDAO (or whatever) that is something like getChildByParentAndName(Parent parent, String name) which has the hql "from child c where c.parent=? and c.name=?" etc etc

Alternatively just get the child with the name and then ask it for it's parent...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 25, 2006 12:18 pm 
Newbie

Joined: Tue Feb 22, 2005 5:27 am
Posts: 10
Thank you for the answer.

Sure, your answer is what I finally did, and I agree with the fact that the parent has still 3 childrens...

But in the first verison of my application I had to display all parents with all the childrens so I had a simple query :

Code:
from Parents;


and the lazy loading gets me all the children...


Now, in the second version, I need to display the children only if there name is :name, I though HBN had a solution to change my query without having to change my code...

Maybe with the "where" or "subselect" attribute in the parent's mapping ?

Quote:
<set name="children" where="name = :name">
...
</set>


But I can't figure out to set a parameter...


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