If you want to do it in HQL, it's pretty much like SQL: SELECT
COUNT(*) FROM...
How efficient COUNT is will depend on how your query aligns with the
available indexes, I think. A good DBMS will not fetch rows if it can
find a cheaper way (such as rummaging in the indexes). Get your DBMS
to give you the query plan it creates for your COUNT query, to see how
costly it is likely to be and whether there is anything reasonable to
be done about that.
But you're using the Criteria style in the referenced code, and you
may want to see if you can just build the query that way, get the
count, and then set your window to start fetching samples. I have not
done this, but it's discussed on StackOverflow:
http://stackoverflow.com/questions/2160259/count-in-hibernate-criteria
You should also consider transaction boundaries (and I am of no
particular help there!) and their effect on the eventual accuracy of
the initial count. You may have to arrange transactions carefully or
accept that the count is merely an estimate.
--
Mark H. Wood
Lead Technology Analyst
University Library
Indiana University - Purdue University Indianapolis
755 W. Michigan Street
Indianapolis, IN 46202
317-274-0749
www.ulib.iupui.edu