我有以下 Hibernate-Query,到目前为止运行良好。
public static List<PerformanceLog> getStatistics() {
return entityManager().createQuery(
"SELECT NEW de.veliqi.selperf.model.PerformanceLog(o.useCase, o.description, o.totalTime) FROM PerformanceLog o",
PerformanceLog.class).getResultList();
}
但是我需要获取 o.totalTime 的平均值,但我不知道如何做。 使用以下语句会引发异常,显示“org.hibernate.hql.internal.ast.QuerySyntaxException:无法在类上找到适当的构造函数”。
"SELECT NEW de.veliqi.selperf.model.PerformanceLog(o.useCase, o.description, AVG(o.totalTime)) FROM PerformanceLog o GROUP BY o.useCase"
正确的做法是什么?
最佳答案
基本查询语法不正确,无法选择 o.useCase 和 o.description 不使用任何一个,对它们使用聚合器,或者将它们两者包含在 group by 子句中
要修复此问题,请将 group by 子句更改为:
GROUP BY o.useCase, o.description
关于mysql - 如何在 MySQL 中使用 Hibernate HQL 和 AVG-Function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16721555/