首页 > 解决方案 > 日期范围之间的 Spring Data JPA 返回 null

问题描述

我正在使用本机查询作为运算符之间的日期,但它返回 null。当我用静态日期运行它时

@Query(value ="SELECT  a.name,m.title  from meetings m ,attendies a where m.id =a.m_aid and (start_time BETWEEN '2021-04-03 11:00:16' AND '2021-04-03 11:30:15' )",nativeQuery = true)
public List<Object[]> findConflictMettinds( Date startTime, Date endTime );

能够获取数据,但是当尝试使用动态值返回时,它总是返回 null。以下是我迄今为止尝试过的不同方法

@Query(value ="SELECT  a.name,m.title  from meetings m ,attendies a where m.id =a.m_aid and (m.start_time BETWEEN ?1 AND ?2 )",nativeQuery = true)
public List<Object[]> findConflictMettinds(@Param("startTime") Date startTime,@Param("endTime") Date endTime );                                          @Query(value ="SELECT  a.name,m.title  from meetings m ,attendies a where m.id =a.m_aid and (m.start_time BETWEEN :startTime AND :endTime )",nativeQuery = true)
public List<Object[]> findConflictMettinds(@Param("startTime") Date startTime,@Param("endTime") Date endTime );

标签: springspring-boothibernatespring-mvcspring-data-jpa

解决方案


@Query(value ="SELECT  a.name,m.title  from meetings m ,attendies a where m.id =a.m_aid and (m.start_time > :startTime AND m.start_time <:endTime )",nativeQuery = true)
public List<Object[]> findConflictMettinds(@Param("startTime") String startTime,@Param("endTime") String endTime );

SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

repository.findConflictMettinds(dateFormatter.format(startTime), dateFormatter.format(endTime))

此外,您不必使用本机查询,但如果有必要,您可以使用给定的示例。您还可以使用 Spring Data JPA 本机查询查找两个日期之间的搜索记录吗?


推荐阅读