2012-05-22 32 views
1

可能重複:
How do we count rows using Hibernate?我們如何使用Hibernate和where子句來計算行數?

我們如何指望用行休眠與where子句?

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

我認爲這個問題已經存在,檢查鏈接 http://stackoverflow.com/questions/1372317/how-do-we-count-rows-using-hibernate – aravindKrishna

+0

INT數=(整數) session.CreateQuery(「select count(*)from table where recName ='any'」)。UniqueResult(); – AurA

回答

1
SELECT Count(*) from DomainClass d where d.someProperty='someValue' 
5

此問題已基本已經回答了計算器:

How do we count rows using Hibernate?

除了使用剛具有突出解決您的where子句中添加作爲附加標準的準則。

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