Hibernate Java - 意外的 AST 节点 AND 和 =

Hibernate Java - Unexpected AST Node AND and =

提问人:Nguyen Pham 提问时间:11/9/2023 最后编辑:Nguyen Pham 更新时间:11/9/2023 访问量:27

问:

我目前正在 Hibernate 5.2.10.Final 上编写查询,并不断遇到意外的 AST 节点错误,查询在这里

SELECT
    DISTINCT cs
FROM
    CashbackSetting cs
LEFT JOIN CashbackRegisterSetting crs ON
    cs.id = crs.cashbackSetting.id
LEFT JOIN crs.affiliates af
LEFT JOIN CashbackDepositSetting cds ON
    cs.id = cds.cashbackSetting.id
LEFT JOIN cds.memberGroups mg
LEFT JOIN CashbackPaymentMethod cpm ON
    cds.id = cpm.cashbackDepositSetting.id
LEFT JOIN cpm.banks b
WHERE
    cs.merchant = :merchant
    AND (cs.currency = :currency)
    AND (:id IS NULL
        OR cs.id = :id)
    AND (coalesce(:cashbackTypes) IS NULL
        OR cs.cashbackType IN (:cashbackTypes))
    AND (cs.startDate >= :startDate
        AND cs.endDate <= :endDate)
    AND (:createdBy IS NULL
        OR cs.createdBy = :createdBy)
    AND (:modifiedBy IS NULL
        OR cs.lastModifiedBy = :modifiedBy)
    AND (:status IS NULL
        OR cs.status = :status)
    AND ((CASE
        WHEN (cs.cashbackType = 'REGISTER') THEN ((CASE
            WHEN (:applyType IS NULL) THEN 1
            WHEN (:applyType = 'CODE_LIST') THEN ((CASE
                WHEN (:useAllAffCode IS NOT NULL) THEN (crs.useAll = :useAllAffCode)
                ELSE (:affId IS NULL
                    OR af.id = :affId)
                END) = 1)
            ELSE (crs.affCodeApplyType = :applyType)
            END) = 1) <!-- error in this line '=' -->
        ELSE (((CASE
            WHEN (:useAllPaymentMethods IS NULL OR :useAllPaymentMethods = FALSE) THEN (
                (:paymentMethod IS NULL OR cpm.paymentMethod = :paymentMethod) 
                AND ((CASE
                    WHEN (:useAllChannelId IS NULL
                        OR :useAllChannelId = FALSE) THEN (:channelId IS NULL
                        OR b.id = :channelId)
                    ELSE (cpm.useAll = TRUE)
                    END) = 1))
            ELSE (cds.useAllPaymentMethods = TRUE)
            END) = 1)
            AND ((CASE <!-- error in this line 'AND' -->
                WHEN (:useAllMemberGroups IS NULL
                    OR :useAllMemberGroups = FALSE) THEN (:groupId IS NULL
                    OR mg.id = :groupId)
                ELSE (cds.useAllMemberGroups = TRUE)
                END) = 1))
        END) = 1)

错误:

2023-11-09 15:27:09.496 ERROR [CashMarket,,,] 23208 --- [  restartedMain] o.h.hql.internal.ast.ErrorCounter        : <AST>:50:25: unexpected AST node: AND
2023-11-09 15:27:09.500 ERROR [CashMarket,,,] 23208 --- [  restartedMain] o.h.hql.internal.ast.ErrorCounter        : <AST>:50:25: unexpected AST node: AND

antlr.NoViableAltException: unexpected AST node: AND
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expressionOrSubQuery(HqlSqlBaseWalker.java:5358)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.elseClause(HqlSqlBaseWalker.java:5431)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.searchedCaseExpression(HqlSqlBaseWalker.java:5267)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.caseExpr(HqlSqlBaseWalker.java:5127)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.arithmeticExpr(HqlSqlBaseWalker.java:3355)
2023-11-09 15:27:07.218  WARN [CashMarket,,,] 23208 --- [onMonitorThread] c.h.s.i.o.impl.InvocationMonitor         : [127.0.0.1]:13782 [dev] [3.7.8] BroadcastOperationHeartbeatsTask delayed 278531 ms
2023-11-09 15:27:07.220 ERROR [CashMarket,,,] 23208 --- [  restartedMain] o.h.hql.internal.ast.ErrorCounter        : <AST>:38:30: unexpected AST node: =

antlr.NoViableAltException: unexpected AST node: =
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expressionOrSubQuery(HqlSqlBaseWalker.java:5358)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.searchedCaseWhenClause(HqlSqlBaseWalker.java:5471)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.searchedCaseExpression(HqlSqlBaseWalker.java:5251)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.caseExpr(HqlSqlBaseWalker.java:5127)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.arithmeticExpr(HqlSqlBaseWalker.java:3355)

因此,根据日志和函数 expressionOrSubQuery() 的来源,第 38 行中的“=”和第 50 行中的“AND”不应该存在,任何人都可以建议我更改吗?

我希望它能够成功构建,因为我能够在工作台上正确运行 sql 版本。显然,HQL 在流程下有许多规则,简单地用 hql 格式解析 sql 不会削减它。

MySQL Spring 休眠 JPA HQL

评论


答: 暂无答案