Convert SQL query to Hibernate query

I need help converting some sql to hibernate sql.

SQL:

String sql = "select time, hour(time) as hour, minute(time) as minute "
           + "from db where time >= DATE_ADD(now(), INTERVAL -24 HOUR) "
           + "group by 2 order by time LIMIT 500";

I am using SQLQuery to add scalars. I tried this for HQL:

String hql = "select time, hour(time), minute(time) from db as O "
           + "where O.time >= :time group by 2 order by O.time";

Query query = session.createQuery(hql);
query.setDate("time", calend.getTime()); //calend is a Calendar object

However, this does not work. The error indicates an hql error.

+4
source share
1 answer

Instead of using, setDatetry using setParameter.

From this link about HQL dates:

"setDate" truncates the HQL date value and ignores hours, minutes, seconds.

Using setParameterwill interpret the values ​​as the parameter that you specified with :variable.

0
source

Source: https://habr.com/ru/post/1598783/


All Articles