JPQL: boolean expression in CASE WHEN needs explicit comparison with TRUE/FALSE?
I had tried the following:
SELECT NEW com.kawoolutions.bbstats.view.ScheduleGameLine(
ga.id AS gid
, ga.scheduledTipoff AS scheduledtipoff
...
, sch.finalScore AS homefinalscore
, sca.finalScore AS awayfinalscore
, CASE WHEN pah.wasWithdrawn OR paa.wasWithdrawn THEN TRUE ELSE FALSE END AS waswithdrawn
)
Last line is important. According to
JPQL/HQL and JPA/Hibernate: boolean expression in select constructor expression not working (unexpected AST node: AND, NPE, HqlSqlWalker.setAlias)?
the CASE WHEN is needed. However when running this, Hibernate fails with
Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node: . near line 19, column 18
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:261)
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(AbstractSessionIm开发者_开发知识库pl.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)
It obviously isn't supported by JPA/Hibernate's grammar, but as Java and DB programmer I'd expect such expressions to work, like if ( hasHitHard ) ... and/or SELECT was_withdrawn...
Why has it been made this way or why isn't it supported as I expected?
Im not sure whether AS operator is supported by the select clause but in this case it is redundant anyway. As for the CASE operator maybe simple pah.wasWithdrawn OR paa.wasWithdrawn would be good enough:
SELECT NEW com.kawoolutions.bbstats.view.ScheduleGameLine(
ga.id,ga.scheduledTipoff,...,
sch.finalScore, sca.finalScore,
pah.wasWithdrawn OR paa.wasWithdrawn
) from ...
精彩评论