-->
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.  [ 11 posts ] 
Author Message
 Post subject: Order-by on a joined class
PostPosted: Tue Jun 15, 2004 4:39 pm 
Newbie

Joined: Mon Jun 07, 2004 9:18 am
Posts: 13
Location: Torino, Italy
Hi,

I have the problem of ordering a set using a property defined in a joined class (Hibernate 2.1.3).
Code:
<class name="Group" ... >
   <set name="items" ... >
      <key ... />
      <one-to-many class="Item"/>
   </set>
   ...
</class>

<class name="Item" ... >
   <many-to-one name="type" class="Type" ... />
   ...
</class>

<class name="Type">
   <property name="description" ... />
   ...
</class>


I'd like to retrieve Groups with their Items ordered by the Type's Description, specifying this in the mapping files.

I'd like to avoid to specify a sort attribute and a comparator for the items, to save CPU cycles (items can be numerous, order of 10,000), and also because the comparator must compare the whole item state, not only the field I'd like to use for ordering (otherwise calling add() to the set will behave strangely).

I have searched this forum and google, but I could not find any solution for this.
Is it possible to tell hibernate to order the items like described ? Or it is a missing feature ? Or am I going the wrong way ?
Has anyone had this problem before, solved it and willing to share the solution ?

Something on the lines of this, maybe ?

Code:
<set name="items" ... hql-order-by="type.description asc">


Thanks in advance,

Simon


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 17, 2004 7:20 am 
Newbie

Joined: Fri Nov 28, 2003 9:08 am
Posts: 19
Location: Brasil
I have the same problem.
If anyone one can point the right direction.....

Tks
Felipe


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 17, 2004 9:21 am 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
You can specify to sort a set using a comparator class - see the sort attribute shown at http://www.hibernate.org/hib_docs/reference/en/html/collections.html#collections-mapping.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 17, 2004 10:08 am 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
Sorry, I really should stop posting when I'm tired.

If you want to avoid using a comparator, then you can use the order-by attribute, where you specify the columns to order by. Since your item-type will be pulled out in the same query as your item, through a join, you should be able to specify that your items sort by their item-type description.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 17, 2004 6:20 pm 
Newbie

Joined: Mon Jun 07, 2004 9:18 am
Posts: 13
Location: Torino, Italy
Hi sdknott,

thanks for the suggestion; but AFAIK, order-by takes a SQL expression that refers to a column of the table where Items are stored, where instead I need a column of the table where Types are stored.

Regards,

Simon


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 18, 2004 4:46 am 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
Hmm, so it seems! I assumed you could order-by on the joined table columns as well, but it seems not.

You might want to have a look at using filters at the bottom of the sorting section on file://localhost/C:/code/java/osource/hibernate-2.1/doc/reference/en/html/collections.html#collections-sorted. If you lazily load your collection and then use a filter, it might give you what you're after.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 18, 2004 4:47 am 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
Hmm try this URL which doesn't point to my local harddisk :)

http://www.hibernate.org/hib_docs/reference/en/html/collections.html#collections-sorted


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 18, 2004 5:11 am 
Newbie

Joined: Mon Jun 07, 2004 9:18 am
Posts: 13
Location: Torino, Italy
Hi sdknott,

Yes, I have read that section. My understanding is that it returns a new sorted set, but it does not order the one in the object graph.

Code:
Set sortedItems = session.filter(group.getItems(), "order by this.type.description");


After this, group.getItems() is still unordered, isn't it ?

Simon


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 23, 2004 11:39 am 
Newbie

Joined: Mon Jun 07, 2004 9:18 am
Posts: 13
Location: Torino, Italy
I am still clueless on this issue, so should I open a JIRA request for enhancement ?

Or is it possible to achieve it in Hibernate already ?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 23, 2004 11:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Its possible; use a query or a filter.

I also have as a TODO: "enhance order-by to support embedded subselects"


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 23, 2004 11:52 am 
Newbie

Joined: Mon Jun 07, 2004 9:18 am
Posts: 13
Location: Torino, Italy
gavin wrote:
Its possible; use a query or a filter.


Am I correct in saying that the filter will create an ordered copy and leave unordered the set attached to the main object ?

And that using the Query API I should put the order-by clause in the HQL statement ?

I hoped it was possible to specify the ordering in the mapping, like it is possible now with the order-by attribute.

gavin wrote:
I also have as a TODO: "enhance order-by to support embedded subselects"


Okay, so I won't file a JIRA item.

Thanks for your time Gavin.


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