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
Function sum with HQL language
提问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 HQL
hibernate language, but I can't find anything!
我有一个问题(对不起我的英语,我正在学习)!我到处去寻找如何使用命令sum(column)
与HQL
休眠的语言,但我无法找到任何东西!
I can do it with SQL
language. 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));