• 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

Get only the newest 5 items

 
clojure forum advocate
Posts: 3479
Mac Objective C Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
I want to get from the database only the newest 5 books.
My first apprach was to get all the books and then use subList( ) method.
It is abvious not a performant way.
(I'm using Hibernate3.2.2)
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it comes down to the database, and not Hibernate. And getting the newest 5 is not a common query directly with a databse. Meaning without you building it into you model.

So as an example, if you had a Date/Time field that was date/time inserted, then you could do a query to get back 5 record, and it is ordered in reverse chronological order by that date/time field.

Mark
 
Hussein Baghdadi
clojure forum advocate
Posts: 3479
Mac Objective C Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My Book table has RELEASING_DATE column.
But actually I don't know how to get a specific number of rows (sorry, I'm not SQL pro). :roll:
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because TOP 5 is not a relational concept it isn't easily expressed in terms of standard (portable) SQL. We need to know what database you are using so that that we can use the appropriate extension. For example, Oracle:


MS T-SQL:


MySql (I'm guessing a bit here):

[ April 23, 2007: Message edited by: Peer Reynders ]
 
Hussein Baghdadi
clojure forum advocate
Posts: 3479
Mac Objective C Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since I'm using Hibernate, Can I employ HQL ?
(BTW, I'm using MySQL4.X);
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,,

use setMaxResults(int maxResults) in session.createCriteria().setFirstResult(int firstResult)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic