2015-07-21 207 views
4

我需要幫助將一些sql轉換爲hibernate sql。將SQL查詢轉換爲Hibernate查詢

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"; 

我用的SQLQuery加標量。我試過這個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 

但是,這是行不通的。錯誤表示這是一個hql錯誤。

+0

爲您只使用一個表,嘗試不使用表的別名。 –

+0

我仍然得到相同的錯誤,如果我沒有表別名。 –

+0

嘗試'GROUP BY小時(時間)',看看是否有幫助。 – knittl

回答

0

而不是使用setDate的請嘗試使用setParameter

this link about HQL dates:

「的setDate」 將截斷HQL日期值,並忽略時,分,秒。

使用setParameter會將這些值解釋爲:variable指示的參數。