2017-05-27 59 views
0

我在使用hibernate查詢嘗試從數據庫檢索當天日期之前的記錄時遇到了一個問題。當前日期查詢不起作用HQL

我的查詢是。

Query q = getSession().createQuery("FROM News n where n.to_published_date<= 
      current_date order by n.to_published_date desc"); 
      return q.list(); 

此查詢是從數據庫獲取所有記錄,而我需要記錄在今天的日期之前。

+0

在今天的日期會給你所有,不是嗎?你已經使用了<=',而不是使用'<'跳過今天的日期記錄。 –

+0

對我沒有用,我想在當前時間之前記錄所有記錄2017/5/27 1:19:11 @N00bPr0grammer –

回答

0

你可以嘗試這樣的事情,如下所示?

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
String frmDate = format.parse(current_date); 

Query q = getSession().createQuery("FROM News n where n.to_published_date<= :frmDate order by n.to_published_date desc") 
         .setTimestamp("frmDate ", current_date); 
return q.list(); 

設置.setTimestamp()應該在你的情況下做到這一點!您可以根據需要更改日期格式 - 您保存對象的方式。

希望這會有所幫助!