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.  [ 5 posts ] 
Author Message
 Post subject: Sorting by pre-defined order
PostPosted: Tue Feb 24, 2004 2:35 pm 
Beginner
Beginner

Joined: Mon Nov 24, 2003 5:39 am
Posts: 26
Location: France
Hello,

Is there a way to group objects by a pre-defined order? Here's an example of what I want to do:

I have an "Document" object with a "type" attribute. I want to retrieve a collection of "Document" objects that are sorted by "type" in the following order: first "Word", secondly "Excel", lastly "PowerPoint".

Thanks.

Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 2:39 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Not in a query. You will have to sort the collection yourself with your own comparator. If the collection is an association you can use the sort attribute in the Hibernate mapping.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 5:41 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Is it what you want
Code:
from Document doc order by doc.type

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 11:12 pm 
Beginner
Beginner

Joined: Sun Oct 26, 2003 11:21 pm
Posts: 27
Or you could make Type a reference to a record in another table, where that record contains a sort order. Map the Type as an association and use something like
Code:
from Document doc order by doc.type.sort


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 2:36 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
The funy thing is, W(ord), E(xcel), P(owerpoint) is not an alphabetical order, so you won't be able to sort it that way in the query not even with custom SQL. hgilde's suggestion is not that bad, too. Might be a good case for a <component> probably, too.


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