• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Criteria

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic