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

发布于 2024-10-17 05:31:13 字数 2362 浏览 1 评论 0原文

我尝试过以下操作:

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

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(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

复古式 2024-10-24 05:31:13

我不确定 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 ...

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 ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文