java 使用 HQL 语言的函数 sum

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37325798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:24:22  来源:igfitidea点击:

Function sum with HQL language

javamysqlhibernatehql

提问by Lucas Zem

I have a question (sorry about my English, I'm learning)! I searched everywhere for how to use the command sum(column)with HQLhibernate language, but I can't find anything!

我有一个问题(对不起我的英语,我正在学习)!我到处去寻找如何使用命令sum(column)HQL休眠的语言,但我无法找到任何东西!

I can do it with SQLlanguage. Example:

我可以用SQL语言来做。例子:

SELECT sum(Column) FROM tablethatIwantthevalues;

but not with HQL Hibernate!

但不适用于 HQL Hibernate!

回答by Niklas S.

You can use aggregation functions in HQL as well as in SQL, take a look at the Hibernate Query Language Manual: https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-aggregation

您可以在 HQL 和 SQL 中使用聚合函数,请查看 Hibernate 查询语言手册:https: //docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql -聚合

回答by Optimaz ID

You can use same query as SQL, try below solution :

您可以使用与 SQL 相同的查询,请尝试以下解决方案:

Session s = OptimazPoolM.getSessionFactory().openSession();
String sumHql = "SELECT SUM(salary) FROM employees WHERE idemployee = 31";
Query sumQuery = s.createQuery(sumHql);
System.out.println(sumQuery.list().get(0));