首页 > 解决方案 > 如何将 LocalDateTime 提供给 jpa/hibernate 查询?

问题描述

我正在我的@RepositoryRestResource 中构建一个查询

查询如下所示:

@Query("Select DISTINCT comp from InsuranceCompany comp " +
            "LEFT JOIN comp.orders ord " +
            "wHERE ord.invoiced = false " +
            "and (:date is null or :date >= ord.completionTime)"
    )
public Page<InsuranceCompany> method(LocalDateTime date, Pageable pageable);

但它会引发以下异常

Failed to convert from type [java.lang.String] to type [java.time.LocalDateTime] for value '2020-02-14T15:50:24'

当我用以下方式调用终点时:

GET /method?date=2020-02-14T15:50:24

标签: postgresqlhibernatespring-bootjpaspring-data-rest

解决方案


标记它以@DateTimeFormat使 Spring 正确转换它:

public Page<InsuranceCompany> method(@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime date, 
                                     Pageable pageable);

推荐阅读