-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Fetching an ordered limit of entities
PostPosted: Thu Nov 20, 2008 4:14 pm 
Beginner
Beginner

Joined: Mon Jul 07, 2008 8:07 pm
Posts: 42
I'm trying to fetch the last 20 entities from Hibernate using the Criteria class. If I call something like this:
Code:
Criteria criteria = session.createCriteria(MyEntity.class);
criteria.setMaxResults(20);

I'll get 20 results back, but the results will start from the beginning of the table, rather than the end of the table. How do I reverse this? I've looked into criteria.addOrder(), but it seems that utilizing that will just format the entities before I get them by calling criteria.list();

What should I do?[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2008 7:43 pm 
Beginner
Beginner

Joined: Mon Jul 07, 2008 8:07 pm
Posts: 42
Scratch that, addOrder does exactly what I need.

Code:
criteria.addOrder(Order.desc("postDate"));
criteria.setMaxResults(20);
criteria.list();


Yields a List of the most recent posts to the database, sorted by the date of the posts, descendingly :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.