1

Possible Duplicate:
How do we count rows using Hibernate?

How do we count rows using Hibernate with where clause?

select count(*) from table where recName = 'any'
Community
  • 1
  • 1
Abdul
  • 577
  • 1
  • 5
  • 21
  • 1
    I think this question already available,check the link http://stackoverflow.com/questions/1372317/how-do-we-count-rows-using-hibernate – aravindKrishna May 22 '12 at 09:40
  • int count = (Integer) session.CreateQuery("select count(*) from table where recName = 'any'").UniqueResult(); – AurA May 22 '12 at 10:06

2 Answers2

5

This questions was basically already answered on stackoverflow:

How do we count rows using Hibernate?

In addition to the solution using Projections you just have to add your where clause as an additional Criterion to the Criteria.

Criteria criteria = session.createCriteria("Book");
criteria.add(Restrictions.eq("title", "My Title"));
criteria.setProjection(Projections.rowCount());
Number numRows = (Number)criteria.uniqueResult(); 
Community
  • 1
  • 1
Stefan Dorner
  • 171
  • 1
  • 7
1
SELECT Count(*) from DomainClass d where d.someProperty='someValue'
jmj
  • 237,923
  • 42
  • 401
  • 438