Search code examples
hibernatehqlnhibernate-hql

unexpected AST Node


I use the below given HQL Query:

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;

Same query works fine in SQL developer and i am able to see the values. But I am receiving expections when i call this Query through my 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)


Solution

  • HQL does not support the DAYS() function, which is a DB2 specific function. However, from the documentation it should support EXTRACT(), assuming the underlying database supports it, which is the case for DB2. So, you should be able to replace this portion of your query:

    DAYS(current_date) - DAYS(B.id.enrolledDate)
    

    with this:

    EXTRACT(EPOCH FROM current_date) - EXTRACT(EPOCH FROM B.id.enrolledDate) / 60*60*24
    

    Extracting the epoch for a date yields the number of seconds since Jan 1, 1970. We then convert this to days by dividing by 60*60*24.