简体   繁体   English

如何在 JPA 查询中将 LocalDateTime 转换为 LocalDate?

[英]How to convert LocalDateTime to LocalDate in JPA Query?

I have object with field LocalDateTime dateTime .我有 object 字段LocalDateTime dateTime And I have the following repository method我有以下存储库方法

@Query("select m from MyClass m " +
                "where m.dateTime >= :startDate and m.dateTime <= :endDate")
findAllByTradeTimeBetween(@Param("startDate") LocalDate startDate, 
                          @Param("endDate") LocalDate endDate);

How to get LocalDate of LocalDateTime dateTime in this method.如何在此方法中获取LocalDate LocalDateTime dateTime的 LocalDate。 Now I have an exception现在我有一个例外

Parameter value [2020-01-31] did not match expected type [java.time.LocalDateTime]

How can I fix it?我该如何解决?

It would seem more strict to keep the query in LocalDateTime, and add an API for LocalDate versions.将查询保留在 LocalDateTime 中似乎更严格,并为 LocalDate 版本添加 API。

@Query("select m from MyClass m " +
                "where m.dateTime >= :startDate and m.dateTime < :endDate")
findAllByTradeTimeBetween(@Param("startDate") LocalDateTime startDate, 
                          @Param("endDate") LocalDateTime endDate);

findAllByTradeTimeBetween(LocalDate startDate, LocalDate endDate) {
    return findAllByTradeTimeBetween(startDate.atStartOfDay(),
                                     endDate.plusDays(1).atStartOfDay());
}

This opens the API for some dubious usage (using LocalDateTimes).这将打开 API 用于某些可疑用途(使用 LocalDateTimes)。

You can use an javax.persistence.AttributeConverter to automate the conversion between the Date formats.您可以使用 javax.persistence.AttributeConverter 来自动执行日期格式之间的转换。 Here's the Converter class which will automatically convert the LocalDateTime object to LocalDate and viceversa:这是转换器 class,它将自动将 LocalDateTime object 转换为 LocalDate,反之亦然:

@SuppressWarnings("UnusedDeclaration")
    @Converter(autoApply = true)
    public class LocalDateConverter implements AttributeConverter<LocalDateTime, LocalDate> {
        @Override
        public LocalDate convertToDatabaseColumn(LocalDateTime localDateTime) {
            //convert localDateTime to LocalDate
        }

        @Override
        public LocalDateTime convertToEntityAttribute(LocalDate localDate) {
            // convert localDate to LocalDateTime
        }
    } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 LocalDate 值查询 JPA LocalDateTime 字段? - How to query JPA LocalDateTime field with a LocalDate value? 如何使用 LocalDate 查询 LocalDateTime? - How to query LocalDateTime with LocalDate? JPA-QL 查询查找在 LocalDate startDate 和 LocalDate endDate 之间具有 LocalDateTime 时间戳的所有实体 - JPA-QL query find all entities with LocalDateTime timestamp between LocalDate startDate and LocalDate endDate 将LocalDate转换为LocalDateTime或java.sql.Timestamp - Convert LocalDate to LocalDateTime or java.sql.Timestamp 使用 Spring Boot 将 json LocalDate 转换为 LocalDateTime - Convert json LocalDate to LocalDateTime using Spring boot 如何使用类 LocalDate 和 LocalDateTime 的抽象? - How to use abstraction with classes LocalDate and LocalDateTime? Spring JPA 实体映射 LocalDate 和 LocalDateTime 字段到 Sql 服务器 - Spring JPA entity mapping LocalDate and LocalDateTime field to Sql Server 如何从 LocalDate 和 LocalDateTime 中提取纪元? - How to extract epoch from LocalDate and LocalDateTime? 如何使用 JPA 持久化 LocalDate? - How to persist LocalDate with JPA? 如何指定一个字段,以便在查询时使用字段的Joda LocalDate或LocalDateTime值将其映射到同一列? - How to specify a field, so that when we query, using Joda LocalDate or LocalDateTime value for the field, it is mapped to same column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM