Hibernate/HQL/JPQL:CASE WHEN ... THEN NULL ELSE ... END (ClassCastException) 有什么问题?

发布于 2024-10-06 17:24:20 字数 2867 浏览 5 评论 0原文

我在 SELECT 中有以下 JPQL/HQL 片段

...
 MAX(CASE WHEN scf.finalScore = 20 OR scf.finalScore = 0 THEN NULL ELSE scf.finalScore END) AS hi,
 MIN(CASE WHEN scf.finalScore = 20 OR scf.finalScore = 0 THEN NULL ELSE scf.finalScore END) AS lo,
...

来查找最高和最低分数。如果 (scf) 得分或对抗得分为 20 或 0,则这是一场具有特殊结果的游戏,对于 MIN 和 MAX 可以忽略该结果。

Hibernate 抛出异常:

Exception in thread "main" java.lang.ClassCastException: org.hibernate.hql.ast.tree.SqlNode cannot be cast to org.hibernate.hql.ast.tree.SelectExpression
    at org.hibernate.hql.ast.tree.CaseNode.getFirstThenNode(CaseNode.java:44)
    at org.hibernate.hql.ast.tree.CaseNode.getDataType(CaseNode.java:40)
    at org.hibernate.hql.ast.util.SessionFactoryHelper.findFunctionReturnType(SessionFactoryHelper.java:402)
    at org.hibernate.hql.ast.tree.AggregateNode.getDataType(AggregateNode.java:82)
    at org.hibernate.hql.ast.tree.ConstructorNode.resolveConstructorArgumentTypes(ConstructorNode.java:163)
    at org.hibernate.hql.ast.tree.ConstructorNode.prepare(ConstructorNode.java:141)
    at org.hibernate.hql.ast.HqlSqlWalker.processConstructor(HqlSqlWalker.java:996)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2260)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:2121)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1522)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:593)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:244)
    at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:254)
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
    at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:124)
    at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
    at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
    at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1770)
    at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:272)
    at tld.standalone.Main.executeJpqlStatement(Main.java:70)
    at tld.standalone.Main.main(Main.java:58)

出了什么问题?是我的代码吗?休眠错误?

问题似乎是 Hibernate 无法处理 THEN 中的 NULL:没有聚合函数的“CASE WHEN scf.finalScore IN (0, 20) THEN NULL ELSE scf.finalScore END AS Playscore”仍然会引发相同的异常。那么,这是一个 Hibernate 错误吗?

I have the following JPQL/HQL snippet in a SELECT

...
 MAX(CASE WHEN scf.finalScore = 20 OR scf.finalScore = 0 THEN NULL ELSE scf.finalScore END) AS hi,
 MIN(CASE WHEN scf.finalScore = 20 OR scf.finalScore = 0 THEN NULL ELSE scf.finalScore END) AS lo,
...

to find the highest and lowest scores. If the score for (scf) or score against are 20 or 0 then it's a game with a special outcome, which is to be ignored for the MIN and MAX.

Hibernate throws an exception:

Exception in thread "main" java.lang.ClassCastException: org.hibernate.hql.ast.tree.SqlNode cannot be cast to org.hibernate.hql.ast.tree.SelectExpression
    at org.hibernate.hql.ast.tree.CaseNode.getFirstThenNode(CaseNode.java:44)
    at org.hibernate.hql.ast.tree.CaseNode.getDataType(CaseNode.java:40)
    at org.hibernate.hql.ast.util.SessionFactoryHelper.findFunctionReturnType(SessionFactoryHelper.java:402)
    at org.hibernate.hql.ast.tree.AggregateNode.getDataType(AggregateNode.java:82)
    at org.hibernate.hql.ast.tree.ConstructorNode.resolveConstructorArgumentTypes(ConstructorNode.java:163)
    at org.hibernate.hql.ast.tree.ConstructorNode.prepare(ConstructorNode.java:141)
    at org.hibernate.hql.ast.HqlSqlWalker.processConstructor(HqlSqlWalker.java:996)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2260)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:2121)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1522)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:593)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:244)
    at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:254)
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
    at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:124)
    at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
    at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
    at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1770)
    at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:272)
    at tld.standalone.Main.executeJpqlStatement(Main.java:70)
    at tld.standalone.Main.main(Main.java:58)

What's wrong? Is it my code? Hibernate bug?

The problem seems to be Hibernate not being able to process a NULL inside a THEN: a "CASE WHEN scf.finalScore IN (0, 20) THEN NULL ELSE scf.finalScore END AS playedscore" without aggregate function still throws the same exception. So, is this a Hibernate bug?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愁以何悠 2024-10-13 17:24:20

JPQL 不允许在聚合函数中使用 CASE,尽管 Hibernate 在语法上没有限制它,因此在 AST 处理期间会抛出异常。

来自 JPA 规范:

aggregate_expression ::=
    { AVG | MAX | MIN | SUM } ([DISTINCT] state_field_path_expression) |
    COUNT ([DISTINCT] identification_variable | state_field_path_expression |
        single_valued_object_path_expression)

state_field_path_expression ::=
    general_identification_variable.{single_valued_object_field.}*state_field

因此,您需要一个本机 SQL 查询来实现这种逻辑。

JPQL doesn't allow CASE in aggregate functions, although Hibernate doesn't restrict it in its grammar, therefore exception is thrown during AST processing.

From JPA Spec:

aggregate_expression ::=
    { AVG | MAX | MIN | SUM } ([DISTINCT] state_field_path_expression) |
    COUNT ([DISTINCT] identification_variable | state_field_path_expression |
        single_valued_object_path_expression)

state_field_path_expression ::=
    general_identification_variable.{single_valued_object_field.}*state_field

So, you need a native SQL query for this kind of logic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文