【问题标题】:Spring JPA method to find entities with beforeAndEqual a date AND afterAndEqual a dateSpring JPA 方法来查找具有 beforeAndEqual 日期和 afterAndEqual 日期的实体
【发布时间】:2018-01-07 20:53:58
【问题描述】:

我有一个实体锦标赛如下:

class Tournament {
    //other attributes
    private LocalDate startDate;
    private LocalDate endDate;
}

这表示从 startDate 到 enddate 持续数天/数月的锦标赛。 我需要使用 Spring JPA 和分页检索今天运行的所有锦标赛,例如 startDate = today

我找到的最接近的是:

@Repository
public interface TournamentRepository extends PagingAndSortingRepository<Tournament, Long> {

    Page<Tournament> findByStartBeforeAndEndAfter(LocalDate date, LocalDate dateCopy, Pageable page); //today's date is passed as date and dateCopy

}

方法调用:

tournamentRepository.findByStartBeforeAndEndAfter(LocalDate.now(), LocalDate.now(), page);

这可以解释为 startDate today,因此如果锦标赛在今天运行且仅持续 1 天,则它不起作用。

有没有更好的方法来使用 Spring JPA 而无需编写自定义查询?

【问题讨论】:

  • 你试过findByStartDateLessThanEqualAndEndDateGreaterThanEqual吗? official documentation 阐明 LessThanEqualGreaterThanEqual 是受支持的关键字。

标签: java spring hibernate spring-mvc spring-data-jpa


【解决方案1】:

您可以对日期使用 LessThan、GreaterThan、LessThanEqual、GreaterThanEqual。

@Repository
public interface TournamentRepository extends PagingAndSortingRepository<Tournament, Long> {

    Page<Tournament> findByGreaterThanEqualStartDateAndLessThanEqualEndDate(LocalDate date, LocalDate dateCopy, Pageable page); //today's date is passed as date and dateCopy

}

【讨论】:

    【解决方案2】:

    CURRENT_DATECURRENT_TIMECURRENT_TIMESTAMP 是 JPA 中的预定义函数。你可以使用它。

    试试这个

    @Repository
    public interface TournamentRepository extends PagingAndSortingRepository<Tournament, Long> {
    
        @Query("Select t from Tournament t where t.startDate <= CURRENT_DATE and t.endDate >= CURRENT_DATE")
        Page<Tournament> findByStartBeforeAndEndAfter(Pageable page);
    
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用@Query 注释并编写自己的查询,如下代码所示。

      @Repository
          public interface TournamentRepository extends PagingAndSortingRepository<Tournament, Long> {
              @Query("select t from Tournament t where t.startDate <=:date and t.endDate >=: dateCopy")
              Page<Tournament> findByStartBeforeAndEndAfter(@Param("date")LocalDate date,@Param("dateCopy") LocalDate dateCopy, Pageable page); //today's date is passed as date and dateCopy
      
          }
      

      你也可以这样试试native query

      @Repository
      public interface TournamentRepository extends PagingAndSortingRepository<Tournament, Long> {
          @Query("select * from tournament where start_date <=:date and end_date >=: dateCopy",nativeQuery=true)
          List<Tournament> findByStartBeforeAndEndAfter(@Param("date")LocalDate date,@Param("dateCopy") LocalDate dateCopy); //today's date is passed as date and dateCopy
      
      }
      

      【讨论】:

        猜你喜欢
        • 2021-08-07
        • 1970-01-01
        • 1970-01-01
        • 2017-11-05
        • 1970-01-01
        • 1970-01-01
        • 2019-12-06
        • 2010-09-28
        • 2019-03-09
        相关资源
        最近更新 更多