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? 回答1: 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 . 回答2: In Java i usually need to return int and use this form: int count = ((Long)getSession().createQuery("select count(*) from Book").uniqueResult()).intValue(); 回答3: