*
The moose likes Object Relational Mapping and the fly likes Get only the newest 5 items Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Get only the newest 5 items" Watch "Get only the newest 5 items" New topic
Author

Get only the newest 5 items

Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

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)
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17256
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

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:
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
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
Bartender

Joined: Nov 08, 2003
Posts: 3479

Since I'm using Hibernate, Can I employ HQL ?
(BTW, I'm using MySQL4.X);
saranga rao
Ranch Hand

Joined: Apr 24, 2007
Posts: 49
hi,,

use setMaxResults(int maxResults) in session.createCriteria().setFirstResult(int firstResult)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get only the newest 5 items