-->
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: How to return only like 20 records?
PostPosted: Sun Jun 08, 2008 12:34 pm 
Newbie

Joined: Sun Mar 05, 2006 12:43 pm
Posts: 5
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.2.6

Code between sessionFactory.openSession() and session.close():

return getHibernateTemplate().find("from Tagging tagging order by tagging.count desc");



Name and version of the database you are using:Mysql 5

Hi guys,

I need to return only like 20 records from a table. I know that there's a method in hibernate template which can set the max return record like below :

getHibernateTemplate().setMaxResults(20);

However, I guess this method is global as when I tried to return other records in the other table, it's also returned 20 records which I want to return all the record. I would appreciate very much if u anyone could advice me on this.

regards,
Mark


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 08, 2008 3:33 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Think about using the Hibernate Criteria API. It will make your life alot easier, and doing this type of pagination of results will make your life alot easier.

Here's a quick and simple tutorial on the topic of the Hibernate Criteria API, along with lots of Hibernate3 code and exmaples:

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=09howtousethecriteriaapi

Your queries will be greatly simplified, and you can have more control, programatically, of how many results are returned on a page or resultset:

Code:
public class FindFirstFive {
public static void main(String args[]) {
  User user = new User();
  Session session = HibernateUtil.beginTransaction();
  Criteria criteria = session.createCriteria(User.class);
  criteria.setFirstResult(0);
  criteria.setMaxResults(5);
  List results = criteria.list();
  HibernateUtil.commitTransaction();
  for (int i = 0; i<results.size(); i++) {
    System.out.println(results.get(i).toString());
  }
}
}

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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.