gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:55:16 25 4
gpt4 key购买 nike

我有一个 JPQL 语句来返回体育比赛时间表:

SELECT NEW com.kawoolutions.bbstats.view.ScheduleGameLine(
ga.id AS gid
, ga.scheduledTipoff AS scheduledtipoff
...
, sch.finalScore AS homefinalscore
, sca.finalScore AS awayfinalscore
, sch.finalScore IS NOT NULL AND sca.finalScore IS NOT NULL AS hasfinalscore
)

我希望最后一个表达式( bool 值)计算为 bool 值,以指示游戏的最终得分是否已完全报告(两个 Score 类型的实体,此处 sch 和 sca 表示主场和客场得分)。但是,Hibernate 失败并出现异常:

11.02.2011 18:40:16 org.hibernate.hql.ast.ErrorCounter reportError
SCHWERWIEGEND: <AST>:17:32: unexpected AST node: AND
Exception in thread "main" java.lang.NullPointerException
at org.hibernate.hql.ast.HqlSqlWalker.setAlias(HqlSqlWalker.java:1000)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.aliasedSelectExpr(HqlSqlBaseWalker.java:2381)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.constructor(HqlSqlBaseWalker.java:2505)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2256)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:2121)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1522)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:593)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:244)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:254)
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)
at com.kawoolutions.bbstats.Main.executeJpqlStatement(Main.java:167)
at com.kawoolutions.bbstats.Main.main(Main.java:154)

当用 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 sch.finalScore IS NOT NULL AND sca.finalScore IS NOT NULL THEN TRUE ELSE FALSE END AS hasfinalscore
)

我真的很想知道为什么这不适用于 CASE WHEN。这是怎么回事?那是我吗?是JPA吗?是 hibernate 吗?错误?

最佳答案

它看起来像是一个指定的行为。 JPA 根本不允许在 SELECT 子句中使用条件表达式,但允许 CASE 表达式。

这里是 JPA Specification 中 JPQL 语法的相关部分:

select_expression ::=    single_valued_path_expression |    scalar_expression |    aggregate_expression |    identification_variable |    OBJECT(identification_variable) |    constructor_expressionconstructor_expression ::=    NEW constructor_name ( constructor_item {, constructor_item}* )constructor_item ::=    single_valued_path_expression |    scalar_expression |    aggregate_expression |    identification_variablescalar_expression ::=    simple_arithmetic_expression |    string_primary |    enum_primary |    datetime_primary |    boolean_primary |    case_expression |    entity_type_expression

关于hibernate - JPQL/HQL 和 JPA/Hibernate : boolean expression in select constructor expression not working (unexpected AST node: AND, NPE、HqlSqlWalker.setAlias)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4972369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com