• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Criteria

 
Patrick McDonogh
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there all,

I have the following code that i am using for pagination.
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(SomeClass.class);
criteria.setMaxResults(20);
criteria.setFirstResult(0);
List results = criteria.list();
tx.commit();
session.close();

The list then contains no more than 20 results of type SomeClass.
What i need to do is figure out how many results there really are.
For instance say there are 35 rows which match the criteria object and i set the maxResults to 20 and the firstResult to 0. The results List will then contain 20 records. But how do i know there were actually 35 records that matched the criteria?

What is the best and most efficient way of doing this if the sql will be run on the database to fetch the records, as when running the criterias sql to get the records the database must know how many results matched the criterias sql.

Could some one help me with this as i dont know the most performant way of running the criteria and finding out how many records match the criteria.

Thanks all and have a nice day.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about a first query to get the number of records, like "Select count(*)"?

I figure you want that just to display on the UI.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic