本文介绍了Hibernate Criteria:使用group by子句投影计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从用户组中按名称执行以下SQL
I want to execute the following SQL
select count(*) as myCount from user group by name;
对于相同的
$ b $,我提出了以下标准b
I came up with the following criteria for the same
DetachedCriteria.ForClass(typeof(UserDTO))
.setProjections(Projections.ProjectionList()
.Add(Projections.rowCount(),"myCount")
.Add(Projections.groupProperty("this.name"));
I将结果返回为count和name的对,我怎么才能得到这个数。
I get the result back as pair of the count and name,How can I get just the count from this.
推荐答案
t认为你可以用Criteria做到这一点,但是使用HQL很容易,它与你的SQL查询完全一样,但是使用实体/属性名称而不是表格/列名。
I don't think you can do it with Criteria, but it's easy with HQL. It's exactly the same string as your SQL query, but with entity/property names instead of table/column ones.
这篇关于Hibernate Criteria:使用group by子句投影计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!