意外的AST节点

时间:2017-03-17 01:21:10

标签: hibernate hql nhibernate-hql

我使用下面给出的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类调用此查询时,我正在接收预期:

  

[3/16/17 21:02:29:624 EDT] 00000027 SystemOut O 406868   [WebContainer:1] ERROR org.hibernate.hql.PARSER - :0:0:   意外的AST节点:查询[3/16/17 21:02:29:626 EDT] 00000027   SystemOut O 406868 [WebContainer:1] DEBUG   org.hibernate.hql.ast.ErrorCounter - :0:0:意外的AST节点:   查询:0:0:意外的AST节点:查询在   org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1312)     在   org.hibernate.hql.antlr.HqlSqlBaseWalker.arithmeticExpr(HqlSqlBaseWalker.java:2749)     在   org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2006)     在   org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:1825)     在   org.hibernate.hql.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1394)     在   org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:553)     在   org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)     在   org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)     在   org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:251)     在   org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)     在   org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:13   [17/16/17 21:02:29:626 EDT] 00000027 SystemOut O 406870   [WebContainer:1] ERROR org.hibernate.hql.PARSER - 右手   二元运算符的操作数为空[美国东部时间3/16/17 21:02:29:627]   00000027 SystemOut O 406870 [WebContainer:1] DEBUG   org.hibernate.hql.ast.ErrorCounter - 二进制文件的右侧操作数   operator为null二元运算符的右侧操作数为null     在   org.hibernate.hql.ast.tree.BinaryArithmeticOperatorNode.initialize(BinaryArithmeticOperatorNode.java:48)     在   org.hibernate.hql.ast.HqlSqlWalker.prepareArithmeticOperator(HqlSqlWalker.java:1033)     在   org.hibernate.hql.antlr.HqlSqlBaseWalker.arithmeticExpr(HqlSqlBaseWalker.java:2756)     在   org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2006)     在   org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:1825)     在   org.hibernate.hql.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1394)     在   org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:553)     在   org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)     在   org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)     在   org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:251)     在   org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)     在   org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134)

1 个答案:

答案 0 :(得分:2)

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将其转换为天数。