This code is now deprecated, as of Hibernate 5.2:
return (Long) sessionFactory.getCurrentSession().createCriteria(mainType)
.setProjection(Projections.rowCount())
.uniqueResult();
What is the proper/idiomatic way to remove the deprecation?
The note in the Hibernate API source code just says:
@deprecated (since 5.2) for Session, use the JPA Criteria
I realize there is something called a JPA Criteria Builder. This unfortunately is mis-named as it does not build JPA Criteria. It can however return a JPA Criteria Query:
CriteriaQuery<T> criteriaQuery = sessionFactory.getCriteriaBuilder().createQuery(mainType);
This, however, is a dead-end as the setProjection()
method is missing.
Please note my question is specifically limited to Criteria Query on Hibernate 5.2+. There is a broader, older version of this question (How do we count rows using older versions of Hibernate (~2009)?)