JPQL:CASE WHEN 中的布尔表达式何时需要与 TRUE/FALSE 进行显式比较?
Posted
技术标签:
【中文标题】JPQL:CASE WHEN 中的布尔表达式何时需要与 TRUE/FALSE 进行显式比较?【英文标题】:JPQL: boolean expression in CASE WHEN needs explicit comparison with TRUE/FALSE? 【发布时间】:2011-06-25 23:21:18 【问题描述】:我尝试了以下方法:
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】:我不确定 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 ...
【讨论】:
您似乎没有完全阅读我的帖子。当然支持AS。对于您的其他评论,请参阅链接***.com/questions/4972369/… ...所以它只是关于以上是关于JPQL:CASE WHEN 中的布尔表达式何时需要与 TRUE/FALSE 进行显式比较?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 JPQL 中使用“if else”或“case when”
云计算学习路线图素材课件,msyql中CASE WHEN语法