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

需要的情况。但是,在运行此程序时,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...

为什么它是这样制作的,或者为什么不像我预期的那样支持它?

4

1 回答 1

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 ...
于 2011-02-13T20:28:18.050 回答