JPQL:CASE中的布尔表达式WHEN需要与TRUE / FALSE进行显式比较吗?

时间:2011-02-12 04:47:56

标签: hibernate jpa jpql boolean-expression case-when

我尝试了以下内容:

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
  )

最后一行非常重要。根据

JPQL/HQL and JPA/Hibernate: boolean expression in select constructor expression not working (unexpected AST node: AND, NPE, HqlSqlWalker.setAlias)?

需要的情况。但是在运行时,Hibernate失败并带有

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(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)

JPA / Hibernate的语法显然不支持它,但作为Java和DB程序员,我希望这些表达式可以工作,比如if(hasHitHard)......和/或SELECT was_withdrawn ......

为什么这样做了,或者为什么它不像我预期的那样得到支持?

1 个答案:

答案 0 :(得分:0)

我不确定select子句是否支持AS运算符,但在这种情况下,无论如何它都是冗余的。至于CASE运算符,也许简单的pah.wasWithdrawn或paa.wasWithdrawn就足够了:

SELECT NEW com.kawoolutions.bbstats.view.ScheduleGameLine(
     ga.id,ga.scheduledTipoff,...,
     sch.finalScore, sca.finalScore,
     pah.wasWithdrawn OR paa.wasWithdrawn
) from ...