英文:
hsql, JpaRepository, named parameters to string
问题
Here is the translated code portion you requested:
现在我正在尝试:
public interface DishRepository extends JpaRepository<Dish, Integer> {
@Query("select d from Dish d where d.menu.date >= :startDate and d.menu.date <= :endDate")
List<Dish> getBetween(@Param("startDate") @NotNull LocalDate startDate,
@Param("endDate") @NotNull LocalDate endDate);
Please note that the code snippet you provided contains HTML entities for special characters (e.g., <
for <
, "
for "
), which I have translated into their respective characters. If you need further assistance or have any questions, please feel free to ask.
英文:
Now I am trying
public interface DishRepository extends JpaRepository<Dish,Integer> {
@Query("select d from Dish d where d.menu.date >=:startDate and d.menu.date<=:endDate")
List<Dish> getBetween(@Param("startDate") @NotNull LocalDate startDate,
@Param("endDate") @NotNull LocalDate endDate);
and get an error since
DEBUG org.springframework.web.servlet.FrameworkServlet.logResult:1101 - Failed to complete request: org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [2020-07-02] did not match expected type [java.time.LocalDateTime (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [2020-07-02] did not match expected type [java.time.LocalDateTime (n/a)]
but all is well if
@Query("select d from Dish d where d.menu.date >='2020-07-02' and d.menu.date<='2020-07-03'")
How to convert named parameters to strings (:startDate -> '2020-07-02')
?
Thanks for any help.
答案1
得分: 1
你应该使用LocalDateTime作为参数。你可以通过以下方式从LocalDate创建LocalDateTime并包含时间:
LocalDateTime startOfDay = LocalDateTime.of(startDate, LocalTime.MIN);
LocalDateTime endOfDay = LocalDateTime.of(endDate, LocalTime.MAX);
然后在查询中使用它们:
@Query("select d from Dish d where d.menu.date >= :startDate and d.menu.date <= :endDate")
List<Dish> getBetween(@Param("startDate") @NotNull LocalDateTime startDate,
@Param("endDate") @NotNull LocalDateTime endDate);
英文:
You should use LocalDateTime as parameter. You can create LocalDateTime from LocalDate with time this way
LocalDateTime startOfDay = LocalDateTime.of(startDate, LocalTime.MIN);
LocalDateTime endOfDay = LocalDateTime.of(endDate, LocalTime.MAX);
And use them in query
@Query("select d from Dish d where d.menu.date >=:startDate and d.menu.date<=:endDate")
List<Dish> getBetween(@Param("startDate") @NotNull LocalDateTime startDate,
@Param("endDate") @NotNull LocalDateTime endDate);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论