• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate 2: HQL "order by"

 
John Landon
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need to get 5 most recent books called "The Book" from the db.
I do this right?



My concern is how it does it? If I have thousands of books it MUST get all of them in order to order them for me and give me the 5 recent ones. Otherwise how it knows that these are recent?
So isn't this like not limiting the query at all and then just sorting and getting 5 of them myself? Also, why I need do add "desc" at the end?

Thanks,
John.
 
Christian Dillinger
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A clever database first does the select and then orders the result. You need desc because you want the five newest ones, so you have to order desc(ending). Otherwise you would get the five oldest ones.
 
John Landon
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.
 
John Landon
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christian Dillinger wrote:A clever database first does the select and then orders the result. You need desc because you want the five newest ones, so you have to order desc(ending). Otherwise you would get the five oldest ones.


another question. This returns me what I need but not sorted. How do I sort it?

Thanks.
 
Christian Dillinger
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the generated SQL-Statement and a readable result of your query.
 
John Landon
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ooops my mistake. all those results had the same time. it is just that my database viewer ordered them in a different order (still by the date but if it is the same it is random). Thanks a lot for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic