2017-05-27 37 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()应该在你的情况下做到这一点!您可以根据需要更改日期格式 - 您保存对象的方式。

希望这会有所帮助!