-->
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: Criteria query limiting on child data
PostPosted: Thu Jan 14, 2010 5:36 pm 
Newbie

Joined: Fri Jun 13, 2008 11:36 am
Posts: 13
Okay let's say I have parent and it contains a collection called child and child has a String called childName. Is there a way to make a criteria query that will return all parents that have a child with childName='Larry'? How about one that only returns parents that contain both a childName='Larry' and childName='Dave'?

I thank you in advance.


Top
 Profile  
 
 Post subject: Re: Criteria query limiting on child data
PostPosted: Fri Jan 15, 2010 6:28 am 
Newbie

Joined: Wed Dec 23, 2009 12:38 pm
Posts: 14
You can use something like this.

Criteria crit = session.createCriteria(Parent.class).createAlias("children", "child").add(Restrictions.like("child.name", "first")).add(Restrictions.like("child.name", "second"));


select
this_.id as id0_1_,
this_.name as name0_1_,
child1_.id as id1_0_,
child1_.name as name1_0_,
child1_.PARENT_ID as PARENT3_1_0_
from
MyParent this_,
MyChild child1_
where
this_.id=child1_.PARENT_ID
and child1_.name like ?
and child1_.name like ?


parent has many children while each child has a parent


Top
 Profile  
 
 Post subject: Re: Criteria query limiting on child data
PostPosted: Fri Jan 15, 2010 11:11 am 
Newbie

Joined: Fri Jun 13, 2008 11:36 am
Posts: 13
Thanks.

That looks like the correct solution for the first case. [ Criteria crit = session.createCriteria(Parent.class).createAlias("children", "child").add(Restrictions.like("child.name", "first")) ] would return parents with a child named "first"

But based on the sql you wrote that doesn't seem to work for the second case. It looks like it would return parents with a child named firstsecond, but it would not return a parent with one child name first and another named second.


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.