JPQL:CASE WHEN 中的布尔表达式需要与 TRUE/FALSE 进行显式比较?
我尝试过以下操作:
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 和 JPA/ Hibernate:选择构造函数表达式中的布尔表达式不起作用(意外的 AST 节点:AND、NPE、HqlSqlWalker.setAlias)?
需要 CASE WHEN。然而,当运行这个时,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 ..为什么
这样做或者为什么没有像我预期的那样得到支持?
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
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(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)
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定 select 子句是否支持 AS 运算符,但在这种情况下它无论如何都是多余的。至于 CASE 运算符,也许简单的 pah.wasWithdrawn 或 paa.wasWithdrawn 就足够了:
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: