How do we count rows using older versions of Hibernate (~2009)?

craftsman picture craftsman · Sep 3, 2009 · Viewed 246.8k times · Source

For example, if we have a table Books, how would we count total number of book records with hibernate?

Answer

Salandur picture Salandur · Sep 3, 2009

For older versions of Hibernate (<5.2):

Assuming the class name is Book:

return (Number) session.createCriteria("Book")
                  .setProjection(Projections.rowCount())
                  .uniqueResult();

It is at least a Number, most likely a Long.