-->
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: Select Bean with unique property using Criteria
PostPosted: Fri Mar 03, 2006 4:30 pm 
Beginner
Beginner

Joined: Thu Jul 08, 2004 2:21 pm
Posts: 20
Location: Toronto
I am trying to express the following query using Criteria :
Code:
from Cat as cat group by cat.name

So simply select all cat's with unique name.

And no luck so far.

Code:
Criteria cr = session.createCriteria(Cat.class);
cr.setProjection(Projections.groupProperty("name"));


Will return only names, when I need the whole Cat.

I think that I am missing something, it look like a trivial task to me.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 05, 2006 7:10 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Group isn't the same as distinct. If you "select * from cat group by name" in SQL, you'll get an error. You could "select name from cat group by name", or "select sum(age) from cat group by name", but you can't do what you're trying to do.

You want "from cat c where (select count(*) from cat group by name) = 1". If you can't get the criteria for that working, post again and I'll work it out for you. (Hint: subqueries are done using DetachedCriteria)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 05, 2006 7:16 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Oops. Obviously I meant "from cat c where c.name = (select count(c2.*) from cat c2 where c2.name = c.name) = 1"


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.