1

I am using HQL with DATE_ADD (mysql function) as below

String hql = 
        "select count(*) " +
        "from ProgramGroupEvent as eventLog " +
        "where eventLog.id= :id" +
        "and DATE_ADD( eventLog.eventDate, INTERVAL :interval MINUTE) > now()"; 


    long count = 0;

    try {
        count = ((Long)sess.createQuery( hql )
            .setLong( "id", id)
            .setInteger("interval", interval )
            .iterate().next()).longValue();

    } catch (HibernateException e) {
        e.printStackTrace();
    }

But hibernate throws a token error as below

antlr.NoViableAltException: unexpected token: :
at org.hibernate.hql.internal.antlr.HqlBaseParser.identPrimary(HqlBaseParser.java:4016) ~[hibernate-core-4.1.7.Final.jar:4.1.7.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.primaryExpression(HqlBaseParser.java:859) ~[hibernate-core-4.1.7.Final.jar:4.1.7.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.atom(HqlBaseParser.java:3390) ~[hibernate-core-4.1.7.Final.jar:4.1.7.Final]

PS: I am using Hibernate 4.1.7

2 Answers 2

3

I did further research and found out the issue. The problem is Hibernate is not able parse the syntax. As per it's expectation, this is not well formed HQL syntax. https://forum.hibernate.org/viewtopic.php?f=1&t=982317&view=previous

I fixed by replacing

DATE_ADD( eventLog.eventDate, INTERVAL :interval MINUTE) > now()"; 

With

time_to_sec(timediff( now() , eventLog.eventDate )) < :seconds"; 

Hopefully, this can help for someone

Sign up to request clarification or add additional context in comments.

Comments

1

Your query require two parametrs

  • pharmacyOid
  • interval

But in your query parameters id and interval are used.

Your query should look:

count = ((Long)sess.createQuery( hql )
        .setLong( "pharmacyOid", id)
        .setInteger("interval", interval )
        .iterate().next()).longValue();

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.