Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 437
craftsman Avatar asked Sep 03 '09 09:09

craftsman


1 Answers

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.

like image 182
Salandur Avatar answered Sep 28 '22 06:09

Salandur