【问题标题】:current date query not working HQL当前日期查询不起作用HQL
【发布时间】:2025-12-21 13:50:06
【问题描述】:

我在尝试使用休眠查询从数据库中检索今天日期之前的记录时遇到问题。

我的查询是。

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

此查询正在从数据库中获取所有记录,而我需要今天日期之前的记录。

【问题讨论】:

  • 今天的日期之前会给你所有的,不是吗?您使用了&lt;=,而不是使用&lt; 来跳过今天的日期记录。
  • 对我不起作用,我想要当前时间 5/27/2017 1:19:11 PM@N00bPr0grammer 之前的所有记录

标签: hibernate hql


【解决方案1】:

你能试试这样吗,如下图所示?

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() 应该可以解决您的问题!您可以根据需要更改日期格式 - 保存 Date 对象的方式。

希望这会有所帮助!

【讨论】: