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.  [ 7 posts ] 
Author Message
 Post subject: Runtime ordering of named query results
PostPosted: Tue Oct 04, 2005 6:07 pm 
Regular
Regular

Joined: Fri Mar 26, 2004 11:45 am
Posts: 75
Greetings.

I need to be able to change the ordering of the result returned by a named query at rutime. Initially I was trying to createFilter on the collection returned by the query, but that didn't work because collection was not persistent and Hibernate was throwing an exception.

I would like to avoid creating similar HQL queries with different ORDER BY clauses.

Is there an elegant way to order named query result at runtime? Or is there a way to add a criterion to a loaded named query?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 12:24 pm 
Senior
Senior

Joined: Wed Aug 17, 2005 12:56 pm
Posts: 136
Location: Erie, PA (USA)
You can use Collections.sort(...) to sort a collection. You need a comparator. There is code in Commons that may help:

i.e.
ComparatorChain mSort = new ComparatorChain();
mSort.addComparator(new BeanComparator("sortProp1"), false);
mSort.addComparator(new BeanComparator("sortProp2"), false);
Collections.sort(dataSet, mSort);

We have used this several places where we didn't want to duplicate query code just to put the results into a different order. While we haven't had any problems, I would think that very large collections could pose some problems.

Curtis ...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 5:36 pm 
Regular
Regular

Joined: Fri Mar 26, 2004 11:45 am
Posts: 75
This may not work for me, because collections returned by the query may be rather large. I would prefer to be able to edit the query by specifying the "order by" clause before running it on the database.

Is there a way to adjust named query by appending "order by" clause at runtime?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 10:12 am 
Senior
Senior

Joined: Wed Aug 17, 2005 12:56 pm
Posts: 136
Location: Erie, PA (USA)
Not that I'm aware of using HQL.

Can you use a CriteriaQuery? You can programmatically deal with the order by with those.

Good Luck,
Curtis ...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 10:44 am 
Regular
Regular

Joined: Fri Mar 26, 2004 11:45 am
Posts: 75
Using CriteriaQuery is not an option for me, because I am required to keep all queries separate from Java code, in hbm files.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 1:44 pm 
Senior
Senior

Joined: Wed Aug 17, 2005 12:56 pm
Posts: 136
Location: Erie, PA (USA)
Check out Session.createFilter(...). While I've never used it, it appears to allow adding bits of HQL for ordering. Pg 145 of H3 doc mentions it.

Curtis ...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 3:09 pm 
Regular
Regular

Joined: Fri Mar 26, 2004 11:45 am
Posts: 75
That doesn't work. See my first message in this thread.


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