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

huangapple go评论94阅读模式
英文:

How to convert LocalDateTime to LocalDate in JPA Query?

问题

我有一个带有字段LocalDateTime dateTime的对象。我有以下的存储库方法:

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

在这个方法中如何获取LocalDateTime dateTimeLocalDate。现在我得到了一个异常:

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

我该如何修复这个问题?

英文:

I have object with field LocalDateTime dateTime. And I have the following repository method

@Query(&quot;select m from MyClass m &quot; +
                &quot;where m.dateTime &gt;= :startDate and m.dateTime &lt;= :endDate&quot;)
findAllByTradeTimeBetween(@Param(&quot;startDate&quot;) LocalDate startDate, 
                          @Param(&quot;endDate&quot;) LocalDate endDate);

How to get LocalDate of LocalDateTime dateTime in this method . Now I have an exception

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

How can I fix it?

答案1

得分: 2

似乎更严格的做法是将查询保持在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());
}

这会为某些可疑的用法(使用LocalDateTimes)打开API。

英文:

It would seem more strict to keep the query in LocalDateTime, and add an API for LocalDate versions.

@Query(&quot;select m from MyClass m &quot; +
                &quot;where m.dateTime &gt;= :startDate and m.dateTime &lt; :endDate&quot;)
findAllByTradeTimeBetween(@Param(&quot;startDate&quot;) LocalDateTime startDate, 
                          @Param(&quot;endDate&quot;) 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).

答案2

得分: 0

你可以使用javax.persistence.AttributeConverter来自动转换日期格式。以下是Converter类,它将自动将LocalDateTime对象转换为LocalDate,反之亦然:

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

    @Override
    public LocalDateTime convertToEntityAttribute(LocalDate localDate) {
        // 将localDate转换为LocalDateTime
    }
}
英文:

You can use an javax.persistence.AttributeConverter to automate the conversion between the Date formats. Here's the Converter class which will automatically convert the LocalDateTime object to LocalDate and viceversa:

@SuppressWarnings(&quot;UnusedDeclaration&quot;)
    @Converter(autoApply = true)
    public class LocalDateConverter implements AttributeConverter&lt;LocalDateTime, LocalDate&gt; {
        @Override
        public LocalDate convertToDatabaseColumn(LocalDateTime localDateTime) {
            //convert localDateTime to LocalDate
        }

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

huangapple
  • 本文由 发表于 2020年8月19日 17:05:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63483580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定