-->
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.  [ 6 posts ] 
Author Message
 Post subject: Use properties instead of columns in collection order-by??
PostPosted: Mon Aug 08, 2005 2:13 am 
Beginner
Beginner

Joined: Mon Nov 29, 2004 11:51 am
Posts: 31
Hibernate version: 3.0.5

Hello,

What I would like to see, is the ability to define order-by in the collection mappings with properties rather than db columns. The reason is that I often need to sort collection results on data in a table other than said collection table. For example...
<set name="people" order-by="address.city"/>

There's more of an explanation in this post here from someone asking the same question...
http://forum.hibernate.org/viewtopic.ph ... ed+orderby


I'm in the process of porting an app from another ORM to Hibernate. This is one of the few remaining issues I have because I don't see an easy workaround.
So my questions is, is it possible now (i.e. am I missing something?) or will it be in the near future? I checked the release notes for 3.1 and didn't see anything.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 08, 2005 6:50 am 
Regular
Regular

Joined: Wed May 05, 2004 8:01 am
Posts: 53
You can resign from SQL sorting and use sort="natural" clause. This way you enable Java sorting.

change your people from:
Code:
private Set people = new HashSet();


to
Code:
private Set people = new TreeSet();


make sure your Person class implements Comparable. You'll get your collection sorted by TreeSet itself instead of database. It has one big advantage: you can use any sorting predicates you wish.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 08, 2005 3:25 pm 
Beginner
Beginner

Joined: Mon Nov 29, 2004 11:51 am
Posts: 31
Thanks for your reply. I am aware of some of the options for sorting in Java. But doesn't that mean you have create a Comparator class for each collection you want to sort?

Either way, it seems many people would want to do the ordering on the DB side. So why the SLQ-centric mapping for order-by? We've gone through the great effort of mapping all our relationships, why not use that instead of a simple column name in the mapping file?


Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 08, 2005 10:19 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This is not currently possible, but we have planned to implement that at some point.
Note that for a @OneToMany, @OrderBy does what you want.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 08, 2005 10:21 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
The preferable way is to use an HQL query. (Make more sense for the perf anyway if the ordering is not needed on every collection retrieval).

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 12:49 am 
Beginner
Beginner

Joined: Mon Nov 29, 2004 11:51 am
Posts: 31
Ok, it looks like I have some options for now. Thanks for the info.


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