我们如何使用带有where子句的Hibernate计算行数?

时间:2012-05-22 09:38:00

标签: java hibernate

  

可能重复:
  How do we count rows using Hibernate?

我们如何使用带有where子句的Hibernate计算行数?

select count(*) from table where recName = 'any'

2 个答案:

答案 0 :(得分:5)

这个问题基本上已在stackoverflow上得到解答:

How do we count rows using Hibernate?

除了使用Projections的解决方案之外,您只需将where子句作为标准的附加标准添加。

Criteria criteria = session.createCriteria("Book");
criteria.add(Restrictions.eq("title", "My Title"));
criteria.setProjection(Projections.rowCount());
Number numRows = (Number)criteria.uniqueResult(); 

答案 1 :(得分:1)

SELECT Count(*) from DomainClass d where d.someProperty='someValue'
相关问题