-->
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: How to add dynamic (runtime) order clause for collection?
PostPosted: Tue Jun 20, 2006 2:32 pm 
Beginner
Beginner

Joined: Mon Jan 23, 2006 12:01 pm
Posts: 46
Hi,

I need to write code that would allow for dynamic specification of order-by column for a collection of children of the main object. I tried to add order to criteria object by using the collection alias:

String alias="parent.children.";

Criteria.addOrder(Order.asc(alias+"childColumn"));

But that did not sort the children inside the collection at all.

I found out that I could modify the mapping file (<set order-by="childColumn">), but this is not dynamic. In my case column name ("childColumn" here) can change at runtime.

Do I have any options for implementing this in Hibernate? I would like to do the sorting on the DB side, without writing a custom SQL/HQL query.
Any help on the subject would be appreciated.
Thanks,

Bratek


Top
 Profile  
 
 Post subject: Re: How to add dynamic (runtime) order clause for collection
PostPosted: Tue Jun 20, 2006 10:16 pm 
Beginner
Beginner

Joined: Fri Jun 02, 2006 1:14 am
Posts: 30
Sorry if this is a stupid question, but how are you initializing your collection in your POJO ? Hibernate recommends doing something like this:

Code:
public class MyPOJO {
  Set<ChildPOJO> children = new HashSet<ChildPOJO>();
...

However, this probably won't work, because the HashSet class doesn't preserve the order of its elements. You can fix this problem by using a LinkedHashSet, instead:

Code:
public class MyPOJO {
  Set<ChildPOJO> children = new LinkedHashSet<ChildPOJO>();
...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 10:15 am 
Beginner
Beginner

Joined: Mon Jan 23, 2006 12:01 pm
Posts: 46
Hi,

I gave it a try, just to make sure, but it didn't work. I think there are a couple of issues here at play. For one, when you load objects from DB, I think Hibernate provides its own containers for Collections (those containers are lazy-load aware, etc.). Secondly, even if the Collection preserves the order, there is no guarantee that child objects will be added to Collection according to any order. Since Hibernate loads children with separate SELECT statements, I don't think that anything added to the criteria would help sorting children either. It would have to be some kind of built-in feature, like filters and the like.

Anyway, thanks for trying to help!

Bratek


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 11:09 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Verify if this link helps you in sorting the collection.

http://www.hibernate.org/hib_docs/v3/re ... ons-sorted


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 2:16 pm 
Beginner
Beginner

Joined: Mon Jan 23, 2006 12:01 pm
Posts: 46
Hi,

Thanks for the info. Unfortunately, in my case the search criteria is dynamic (changes between calls), so I can't put it in mapping file, since I don't know which column the collection will be sorted by.

I ran into the createFilter method before, and it looked vaguely promising, but I couldn't figure out how to use it - I'm suspecting it only works for lazy loading, since it seems to make an additional trip to the db to get the collection (and sort it). The catch is that in this example the main object (parent of the collection) must have been already loaded, otherwise where would the collection object reference come from?

Alas, I can't use lazy loading in my project. My code deals with volatile data that can change between calls, so I disabled lazy loading to make sure that I get a 'snapshot' of the data collected all at once.

Bratek


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.