hsql, JpaRepository, 命名参数转为字符串

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

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., &lt; for <, &quot; 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&lt;Dish,Integer&gt; {
      @Query(&quot;select d from Dish d where d.menu.date &gt;=:startDate and d.menu.date&lt;=:endDate&quot;)
      List&lt;Dish&gt; getBetween(@Param(&quot;startDate&quot;) @NotNull LocalDate startDate,
                            @Param(&quot;endDate&quot;) @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(&quot;select d from Dish d where d.menu.date &gt;=&#39;2020-07-02&#39; and d.menu.date&lt;=&#39;2020-07-03&#39;&quot;)

How to convert named parameters to strings (:startDate -&gt; &#39;2020-07-02&#39;) ?
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(&quot;select d from Dish d where d.menu.date &gt;=:startDate and d.menu.date&lt;=:endDate&quot;)
  List&lt;Dish&gt; getBetween(@Param(&quot;startDate&quot;) @NotNull LocalDateTime startDate,
                        @Param(&quot;endDate&quot;) @NotNull LocalDateTime endDate);

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

发表评论

匿名网友

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

确定