-->
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.  [ 4 posts ] 
Author Message
 Post subject: Fetching records in a given order
PostPosted: Wed Feb 01, 2006 6:52 am 
Regular
Regular

Joined: Tue Nov 23, 2004 7:42 am
Posts: 82
Location: London, England
How could one go about writing a query that returns a list of records in the order their IDs were given?

The query "select * from foo where id in (38, 13, 2)" would return those records but in the order 2, 13, 38. This isn't what I want.

Clearly I could loop over my IDs and perform a single fetch for each but this would be inefficient. Any suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 9:04 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
what about order by usage.
Code:
from Foo f order by f.myIDcolumn

If you need only the pk
Code:
select f.myIdColumn from Foo f order by f.myIDcolumn

You should have a look in the Hibernate reference to find more information.

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 9:10 am 
Newbie

Joined: Fri Oct 14, 2005 11:43 am
Posts: 18
I don't think what you want is possible in SQL. Try fetching them in a single db hit and then ordering them in memory.


Regards
Brian


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 9:39 am 
Regular
Regular

Joined: Tue Nov 23, 2004 7:42 am
Posts: 82
Location: London, England
bremmington wrote:
I don't think what you want is possible in SQL. Try fetching them in a single db hit and then ordering them in memory.


Regards
Brian


I think your right. I guess at the end of the day there is a trade of between ordering yourself or a select for each item. Given that we may page the list I'm inclined to go with the fetch for each record as there'll be a maximum of 50 or so fetches each time.

To put this into context, the ID's are coming from a Lucene search where the results are (obviously) weighted. The database behaviour I'd like to avoid renders the weighting useless. I know I could avoid fetching the rows from the database and build the results from my Lucene hits but it's good to verify the data you're getting back is valid and the results do actually exist. In a perfect world I wouldn't need to bother, I'm just covering myself.


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