【问题标题】:Hibernate SQL query compare only date in timestamp fieldHibernate SQL 查询仅比较时间戳字段中的日期
【发布时间】:2019-11-07 10:41:32
【问题描述】:

我必须从 mysql DB 获取基于last_update_At的记录列表。它是 mysql 中的 timestamp 字段。从我的 java 代码中,我以以下格式传递日期。

 Mon May 06 20:05:32 IST 2019

在 mysql last_update_At 中具有以下格式 2019-05-06 19:46:00。我只想在日期比较时获取此记录,而不是与时间比较。

请在下面找到我的休眠查询:

 @Query("select u from User u where u.lastLoginAt <= :thresholdDate")
 public List<User> findLastLoginDaysDifference(@Param("thresholdDate") Date thresholdDate);

使用此查询无法获取记录。

【问题讨论】:

  • 在比较之前将其转换为日期。这是处理仅日期比较的最安全方法

标签: java mysql hibernate date


【解决方案1】:

您可以使用 Hibernate 强制转换运算符:

select u from User u where cast(u.lastLoginAt as date) <= :thresholdDate

【讨论】:

    猜你喜欢
    • 2020-08-18
    • 1970-01-01
    • 2010-09-11
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多