意外的 AST 节点
unexpected AST Node
我使用下面给定的 HQL 查询:
select A.id.customerName,
A.id.customerId,
A.id.IZone,
B.id.accountType,
B.id.accountNumber,
B.id.bankBranch,
(DAYS(current_date)-DAYS(B.id.enrolledDate)) -
(select count(distinct C.id.DWkhol) from Holiday C
where C.id.ICo='01' and
C.id.DWkhol between B.id.enrolledDate and current_date)
from Profile A, Account B
where B.id.accountNumber != ' ' and
A.id.customerId= B.id.customerId;
相同的查询在 SQL 开发人员中运行良好,我能够看到这些值。
但是当我通过 java class:
调用此查询时,我收到了期望
[3/16/17 21:02:29:624 EDT] 00000027 SystemOut O 406868
[WebContainer : 1] ERROR org.hibernate.hql.PARSER - :0:0:
unexpected AST node: query [3/16/17 21:02:29:626 EDT] 00000027
SystemOut O 406868 [WebContainer : 1] DEBUG
org.hibernate.hql.ast.ErrorCounter - :0:0: unexpected AST node:
query :0:0: unexpected AST node: query at
org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1312)
at
org.hibernate.hql.antlr.HqlSqlBaseWalker.arithmeticExpr(HqlSqlBaseWalker.java:2749)
at
org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2006)
at
org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:1825)
at
org.hibernate.hql.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1394)
at
org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:553)
at
org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at
org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at
org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:251)
at
org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
at
org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:13
[3/16/17 21:02:29:626 EDT] 00000027 SystemOut O 406870
[WebContainer : 1] ERROR org.hibernate.hql.PARSER - right-hand
operand of a binary operator was null [3/16/17 21:02:29:627 EDT]
00000027 SystemOut O 406870 [WebContainer : 1] DEBUG
org.hibernate.hql.ast.ErrorCounter - right-hand operand of a binary
operator was null right-hand operand of a binary operator was null
at
org.hibernate.hql.ast.tree.BinaryArithmeticOperatorNode.initialize(BinaryArithmeticOperatorNode.java:48)
at
org.hibernate.hql.ast.HqlSqlWalker.prepareArithmeticOperator(HqlSqlWalker.java:1033)
at
org.hibernate.hql.antlr.HqlSqlBaseWalker.arithmeticExpr(HqlSqlBaseWalker.java:2756)
at
org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2006)
at
org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:1825)
at
org.hibernate.hql.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1394)
at
org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:553)
at
org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at
org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at
org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:251)
at
org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
at
org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134)
HQL 不支持 DAYS()
函数,这是 DB2 特定的函数。但是,从 documentation 来看它应该支持 EXTRACT()
,假设底层数据库支持它,DB2 就是这种情况。因此,您应该能够替换查询的这一部分:
DAYS(current_date) - DAYS(B.id.enrolledDate)
有了这个:
EXTRACT(EPOCH FROM current_date) - EXTRACT(EPOCH FROM B.id.enrolledDate) / 60*60*24
提取日期的纪元会得出自 1970 年 1 月 1 日以来的秒数。然后我们将其除以 60*60*24
。
将其转换为天数
我使用下面给定的 HQL 查询:
select A.id.customerName,
A.id.customerId,
A.id.IZone,
B.id.accountType,
B.id.accountNumber,
B.id.bankBranch,
(DAYS(current_date)-DAYS(B.id.enrolledDate)) -
(select count(distinct C.id.DWkhol) from Holiday C
where C.id.ICo='01' and
C.id.DWkhol between B.id.enrolledDate and current_date)
from Profile A, Account B
where B.id.accountNumber != ' ' and
A.id.customerId= B.id.customerId;
相同的查询在 SQL 开发人员中运行良好,我能够看到这些值。 但是当我通过 java class:
调用此查询时,我收到了期望[3/16/17 21:02:29:624 EDT] 00000027 SystemOut O 406868 [WebContainer : 1] ERROR org.hibernate.hql.PARSER - :0:0: unexpected AST node: query [3/16/17 21:02:29:626 EDT] 00000027 SystemOut O 406868 [WebContainer : 1] DEBUG org.hibernate.hql.ast.ErrorCounter - :0:0: unexpected AST node: query :0:0: unexpected AST node: query at org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1312) at org.hibernate.hql.antlr.HqlSqlBaseWalker.arithmeticExpr(HqlSqlBaseWalker.java:2749) at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2006) at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:1825) at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1394) at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:553) at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281) at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229) at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:251) at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183) at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:13 [3/16/17 21:02:29:626 EDT] 00000027 SystemOut O 406870 [WebContainer : 1] ERROR org.hibernate.hql.PARSER - right-hand operand of a binary operator was null [3/16/17 21:02:29:627 EDT] 00000027 SystemOut O 406870 [WebContainer : 1] DEBUG org.hibernate.hql.ast.ErrorCounter - right-hand operand of a binary operator was null right-hand operand of a binary operator was null at org.hibernate.hql.ast.tree.BinaryArithmeticOperatorNode.initialize(BinaryArithmeticOperatorNode.java:48) at org.hibernate.hql.ast.HqlSqlWalker.prepareArithmeticOperator(HqlSqlWalker.java:1033) at org.hibernate.hql.antlr.HqlSqlBaseWalker.arithmeticExpr(HqlSqlBaseWalker.java:2756) at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2006) at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:1825) at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1394) at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:553) at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281) at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229) at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:251) at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183) at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134)
HQL 不支持 DAYS()
函数,这是 DB2 特定的函数。但是,从 documentation 来看它应该支持 EXTRACT()
,假设底层数据库支持它,DB2 就是这种情况。因此,您应该能够替换查询的这一部分:
DAYS(current_date) - DAYS(B.id.enrolledDate)
有了这个:
EXTRACT(EPOCH FROM current_date) - EXTRACT(EPOCH FROM B.id.enrolledDate) / 60*60*24
提取日期的纪元会得出自 1970 年 1 月 1 日以来的秒数。然后我们将其除以 60*60*24
。