-->
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 API: ordering by collection size
PostPosted: Sat Sep 30, 2006 3:08 am 
Newbie

Joined: Fri Sep 29, 2006 6:32 am
Posts: 1
Welcome,

I am trying to learn Criteria API with demo application.
I have two many-to-many bidirectional association:

class Event {
...
private Set<Person> participants;
...
/* getters and setters */
}

class Person {
...
private Set<Event> events;
...
/* getters and setters */
}

They're mapped with many-to-many almost like in tutorial and everything works
great.

But how can I retrieve list of people ordered by events count?
With HQL it looks something like this

select person from Person person order by events.size

and works.

Using Criteria API I can order only by properties(?). Is the only solution then to sort
the list in memory after retrieval?

Thanks for help,

Mike


Top
 Profile  
 
 Post subject: Yes , You can order only by properties
PostPosted: Wed Feb 21, 2007 7:41 am 
Newbie

Joined: Tue Jan 09, 2007 2:22 pm
Posts: 2
But you can insert a calculated one in the Event object and then you can make criteria queries on it.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 11, 2008 2:28 am 
Newbie

Joined: Tue Jan 30, 2007 5:58 pm
Posts: 14
I have the same problem. I want to sort with the collection size property.

You could use the size function.

This solution can be help:
Code:
from Cat cat where cat.kittens.size > 0


Can we use size function with the Criteria API to sort ?

Do you have any examples ?

Thanks.

I have also read that we can use Projection to solve this problem ? Any ideas about this ?


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.