【问题标题】:Spring Data JPA date query doesn't work for Date been and date greater thanSpring Data JPA 日期查询不适用于日期和日期大于
【发布时间】:2020-11-28 23:10:20
【问题描述】:

我正在尝试使用 Spring data JPA 进行查询,并带有以下查询。但我可以得到任何给定日期之间的归档输出。

@Query(value = "SELECT new com.xxx.DonationByUserTotal(d.club.name, d.club.id , d.club.logo, SUM(d.amount)) " +
        "from Donation d where d.user.id=?1 and d.createdDate  >= ?2 GROUP BY d.club")
List<DonationByUserTotal> findByUserAndFromDate(int User, @Param("fromDate") Date fromDate);

此查询返回给定用户的所有日期,但不在给定日期之间过滤。

@Query("SELECT new com.xxx.DonationByUserTotal(d.club.name, d.club.id , d.club.logo, SUM(d.amount) )" +
        "from Donation d where d.user.id=?1 and d.createdDate BETWEEN ?2 and  ?3  GROUP BY d.club")
List<DonationByUserTotal> findByUserAndBetweenDates(int User,@Param("from")Date fromDate,@Param("to") Date toDate);

尽管两者之间的日期有效,但此查询返回一个空集。

有人可以帮我解决这个问题吗?

PS:

实体类正在使用

@CreatedDate
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false, updatable = false)
private Date createdDate;

传递给查询的参数也带有时间戳。

【问题讨论】:

    标签: java spring spring-boot spring-data-jpa spring-data


    【解决方案1】:

    我能够确定,错误是由于日期格式不同造成的。因为 spring 没有抛出任何错误,所以很难弄清楚。

    【讨论】:

      猜你喜欢
      • 2019-08-13
      • 1970-01-01
      • 2011-11-24
      • 1970-01-01
      • 2020-11-28
      • 2016-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多