Search code examples
mysqlsqlhibernatedatetimehql

Add and Compare Date and Time in HQL


I had created below sql query for my code, I need to convert it to HQL createQuery, I tried a lot. Anyone please tell me how to add datetime to query itself and comparing.

Query query = session.createSQLQuery("select * from schedule s
    where NOW() >= ADDDATE(s.view_datetime, INTERVAL 2 HOUR)");

Solution

  • After many workaround I got answer. It may help others who searching for similar senario..

    Calendar calendar = new GregorianCalendar();
    calendar.setTime(new Date());       
    Date today = calendar.getTime(); 
    
    Query query = session.createQuery("from schedule s where DATE(s.view_datetime) <= :today AND (hour(CURRENT_TIMESTAMP() - s.view_datetime) >= 2)");