Hibernate/HQL/JPQL: what's wrong with a CASE WHEN ... THEN NULL ELSE ... END (ClassCastException)?
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?
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.
精彩评论