-->
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.  [ 3 posts ] 
Author Message
 Post subject: HQL : order query using a Java List ?
PostPosted: Fri Jun 22, 2012 12:25 pm 
Newbie

Joined: Fri Jun 22, 2012 12:17 pm
Posts: 2
Hi all,

i expose my problem :
I have in my model a relation ACTION with this format
ID | ID_ACTION | EVENT_ID
with EVENT_ID a foreign key which is an integer.

i would like now to select in ACTION all the tuples and sort them according to a list of EVENT_ID.
For instance, if i have in ACTIONS the following:
ID | ID_ACTION | EVENT_ID
1 | a | 1
2 | b | 2
3 | c | 2
4 | d | 3

And the sorted list of event_id L=(2,3,1), i would like the previous tuples list to be sorted as follows :
ID | ID_ACTION | EVENT_ID
2 | b | 2
3 | c | 2
4 | d | 3
1 | a | 1

So i wonder if there is a way to parameter a Hibernate Query using a list to order my results. I've never seen such thing before, even in SQL, but i think some people here could help me :)

Thanks a lot !

Cédric


Top
 Profile  
 
 Post subject: Re: HQL : order query using a Java List ?
PostPosted: Sat Jun 23, 2012 9:22 pm 
Beginner
Beginner

Joined: Tue Oct 30, 2007 7:57 am
Posts: 47
The only way I know to do that, is to execute a query that replaces the EVENT_ID with their order position.

In your example: L=(2,3,1), you should substitute EVENT_ID=2 with 1, EVENT_ID=3 with 2 and EVENT_ID=1 with 3.

The ugly part is how to do that, because it will only work if you are using a database capable of doing that (ie Oracle). In oracle you may use CASE WHEN satatements

An example of JPA/Hibernate query if you use Oracle:

Code:
from Action action
order by (
  case
    when action.eventId = 2 then 1
    when action.eventId = 3 then 2
    when action.eventId = 1 then 3
    else 4
   end
)


An alternative option is to order the result in memory after recovering it.


Top
 Profile  
 
 Post subject: Re: HQL : order query using a Java List ?
PostPosted: Sun Jun 24, 2012 11:21 am 
Newbie

Joined: Fri Jun 22, 2012 12:17 pm
Posts: 2
Thank you very much for your answer !


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