我在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 bug?
问题似乎是Hibernate无法在THEN中处理NULL:“当scf.finalScore IN(0,20)THEN NULL ELSE scf.finalScore END AS playedscore”没有聚合函数仍然会抛出相同的情况例外。那么,这是一个Hibernate错误吗?
答案 0 :(得分:1)
JPQL在聚合函数中不允许CASE
,尽管Hibernate不会在语法中限制它,因此在AST处理期间抛出异常。
来自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
因此,您需要针对此类逻辑的本机SQL查询。