Actually, that won't work, since some of my queries have aggregate functions in the select, and these obviously cause the query to generate a different number of results.
I have, however, discovered an alternate approach. By using ScrollableResults I can (inefficiently) hack the row count thus:
Code:
ScrollableResults scrollableResults = query.scroll();
scrollableResults.last();
count = scrollableResults.getRowNumber() + 1;
This seems to work in most cases, although I find that it still fails in queries with inner joins, throwing the following exception:
Code:
org.hibernate.HibernateException: Cannot scroll queries which initialize collections
at org.hibernate.loader.Loader.scroll(Loader.java:1592)
at org.hibernate.hql.classic.QueryTranslatorImpl.scroll(QueryTranslatorImpl.java:1069)
at org.hibernate.impl.SessionImpl.scroll(SessionImpl.java:956)
at org.hibernate.impl.QueryImpl.scroll(QueryImpl.java:62)
at org.hibernate.impl.QueryImpl.scroll(QueryImpl.java:52)
For now I just ignore the exception, since so far this approach seems to work for my purposes. I'm sure that somewhere down the line I will discover a case where this will not work, since it is quite obviously the wrong way to go about doing things, but for now it's the best I can do. Hopefully someone on the Hibernate team will temporarily shed their veil of arrogance long enough to address this problem, or at least explain why they have no intention of doing so.