-->
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: Help with Criteria API and collections needed please
PostPosted: Fri Sep 28, 2007 7:49 am 
Regular
Regular

Joined: Mon Aug 20, 2007 6:47 am
Posts: 74
Location: UK
Say I have the following classes:

class Cat {
List<Kitten> kittens;
}

class Kitten {
String name;
}

How, using the Criteria API can I query for a list of Cats who DO NOT have a kitten with the particular name "Moggy".

The closest I can think of is the following, although I think it returns a list of cats that have 1 or more kittens that are not called Moggy (even ones with a kitten called Moggy).

DetachedCriteria.forClass(Cat.class)
.createCriteria("kittens")
.add(Restrictions.ne("name","Moggy");


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 28, 2007 11:11 am 
Newbie

Joined: Wed Aug 17, 2005 3:02 pm
Posts: 7
first imagine the sql query you'd write to pull this off:
Code:
select * from cats where cat_id not in
(select cat_id from kittens where name = 'Moggy')


so in order to do this with criteria you'll need to perform a subselect:
Code:
DetachedCriteria kittenCriteria = DetachedCriteria.forClass(Kittens.class);
kittenCriteria.add(Restrictions.eq("name", "Moggy")).setProjection(Projections.property("catId");
DetachedCriteria catCriteria = DetachedCriteria.forClass(Cats.class);
catCriteria.add(Property.forName("catId").notIn(kittenCriteria));


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 01, 2007 6:54 am 
Regular
Regular

Joined: Mon Aug 20, 2007 6:47 am
Posts: 74
Location: UK
That's very helpful, thank you!


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.