[英]coverting sql count aggregate function to hibernate criteria
I have the following sql query which i am trying to covert to a hibernate find: 我有以下SQL查询,我试图掩盖到一个冬眠的发现:
select column_1, column_2, count(*)
from data_table
group by column_1, column_2
order by column_1, column_2
;
So far this is the code i have: 到目前为止,这是我拥有的代码:
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(table.class);
detachedCriteria.setProjection(Projections.projectionList()
.add(Projections.groupProperty("column_1"))
.add(Projections.groupProperty("column_2"))
.add(Projections.rowCount()))
.addOrder(Order.asc("column_1"))
.addOrder(Order.asc("column_2"));
For some reason i am getting the following error: ORA-00979: not a GROUP BY expression 由于某种原因,我收到以下错误:ORA-00979:不是GROUP BY表达式
Is this the right way to translate the the sql query using hibernate? 这是使用hibernate转换sql查询的正确方法吗? if not, what would be a better way
如果没有,有什么更好的方法
I am also trying to map the returned count column to a transient property in the model object. 我还试图将返回的count列映射到模型对象中的瞬态属性。 What would be a good way to implement that?
什么是实现该目标的好方法?
Thanks 谢谢
I was able to implement what i was looking for using the code below: 我可以使用以下代码实现我正在寻找的东西:
detachedCriteria.setProjection(Projections.projectionList()
.add(Projections.groupProperty("column_1"))
.add(Projections.groupProperty("column_2"))
.add(Projections.sqlProjection(
"count(*) as counter",
new String[] { "counterproperty" },
new Type[] { Hibernate.LONG }
)))
.addOrder(Order.asc("column_1"))
.addOrder(Order.asc("column_2"));
detachedCriteria.setResultTransformer(Transformers.aliasToBean(modelObject.class));
counterProperty is a transient property i added to the model object counterProperty是我添加到模型对象的瞬时属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.