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 Criteria Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "Criteria" Watch "Criteria" New topic
Author

Criteria

Patrick McDonogh
Ranch Hand

Joined: Oct 13, 2005
Posts: 89
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

Joined: Feb 05, 2001
Posts: 16622

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
 
subject: Criteria
 
Threads others viewed
Parsing crfiteriaFind()- syntax errors?
how to define criteria for search
return DataInfo[]
Threads and DB Access
prepared statements in hibernate
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture