• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate 2: HQL "order by"

 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot.
 
John Landon
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the generated SQL-Statement and a readable result of your query.
 
John Landon
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic