File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Get only the newest 5 items Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply 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: 3334

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: 16622

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: 3334

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: 2906
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 ]

"Don't succumb to the false authority of a tool or model. There is no substitute for thinking."
Andy Hunt, Pragmatic Thinking & Learning: Refactor Your Wetware p.41
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3334

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)
 
 
subject: Get only the newest 5 items
 
Threads others viewed
SCJP 5 books except K&B
What is new in SCEA 5 ?
Book Giveaway!
Infinite parameters
K&B Chapter 1 Question 5
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com