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

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

How to convert LocalDateTime to LocalDate in JPA Query?

问题

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

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

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

  1. 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

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

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

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

How can I fix it?

答案1

得分: 2

似乎更严格的做法是将查询保持在LocalDateTime中,并添加适用于LocalDate版本的API。

  1. @Query("select m from MyClass m " +
  2. "where m.dateTime >= :startDate and m.dateTime < :endDate")
  3. findAllByTradeTimeBetween(@Param("startDate") LocalDateTime startDate,
  4. @Param("endDate") LocalDateTime endDate);
  5. findAllByTradeTimeBetween(LocalDate startDate, LocalDate endDate) {
  6. return findAllByTradeTimeBetween(startDate.atStartOfDay(),
  7. endDate.plusDays(1).atStartOfDay());
  8. }

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

英文:

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

  1. @Query(&quot;select m from MyClass m &quot; +
  2. &quot;where m.dateTime &gt;= :startDate and m.dateTime &lt; :endDate&quot;)
  3. findAllByTradeTimeBetween(@Param(&quot;startDate&quot;) LocalDateTime startDate,
  4. @Param(&quot;endDate&quot;) LocalDateTime endDate);
  5. findAllByTradeTimeBetween(LocalDate startDate, LocalDate endDate) {
  6. return findAllByTradeTimeBetween(startDate.atStartOfDay(),
  7. endDate.plusDays(1).atStartOfDay());
  8. }

This opens the API for some dubious usage (using LocalDateTimes).

答案2

得分: 0

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

  1. @SuppressWarnings("UnusedDeclaration")
  2. @Converter(autoApply = true)
  3. public class LocalDateConverter implements AttributeConverter<LocalDateTime, LocalDate> {
  4. @Override
  5. public LocalDate convertToDatabaseColumn(LocalDateTime localDateTime) {
  6. // 将localDateTime转换为LocalDate
  7. }
  8. @Override
  9. public LocalDateTime convertToEntityAttribute(LocalDate localDate) {
  10. // 将localDate转换为LocalDateTime
  11. }
  12. }
英文:

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:

  1. @SuppressWarnings(&quot;UnusedDeclaration&quot;)
  2. @Converter(autoApply = true)
  3. public class LocalDateConverter implements AttributeConverter&lt;LocalDateTime, LocalDate&gt; {
  4. @Override
  5. public LocalDate convertToDatabaseColumn(LocalDateTime localDateTime) {
  6. //convert localDateTime to LocalDate
  7. }
  8. @Override
  9. public LocalDateTime convertToEntityAttribute(LocalDate localDate) {
  10. // convert localDate to LocalDateTime
  11. }
  12. }

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:

确定