【问题标题】:How to use Hibernate HQL and AVG-Function in MySQL如何在 MySQL 中使用 Hibernate HQL 和 AVG-Function
【发布时间】:2013-05-23 18:43:17
【问题描述】:

我有以下 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: Unable to locate proper constructor on class”。

"SELECT NEW de.veliqi.selperf.model.PerformanceLog(o.useCase, o.description, AVG(o.totalTime)) FROM PerformanceLog o GROUP BY o.useCase"

正确的做法是什么?

【问题讨论】:

    标签: mysql hibernate hql average


    【解决方案1】:

    基本查询语法错误,无法选择 o.useCase 和 o.description 没有任何一个,对它们使用聚合器,或者将它们 BOTH 包含在 group by 子句中

    要修复,请将 group by 子句更改为:

    GROUP BY o.useCase, o.description
    

    【讨论】:

      【解决方案2】:

      感谢中超,

      但这不是问题,因为按一列分组效果很好。

      我发现问题在于 AVG() 函数将 o.totalTime 的类型更改为 Double 值。这就是它找不到合适的构造函数的原因。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-06
        • 2012-08-01
        • 1970-01-01
        • 2012-01-18
        • 2018-03-19
        • 1970-01-01
        • 2023-04-07
        • 2019-03-11
        相关资源
        最近更新 更多