Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Hibernate 2: HQL "order by"

 
Ranch Hand
Posts: 239
  • 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.
 
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: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.
 
John Landon
Ranch Hand
Posts: 239
  • 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: 239
  • 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.
 
He got surgery to replace his foot with a pig. He said it was because of this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic