英文:
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 dateTime
的LocalDate
。现在我得到了一个异常:
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("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 . 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("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).
答案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("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
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论