CrudRepository 按当前时间戳自定义查找

CrudRepository custom find by current timestamp

我有table绑定

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "boundId", unique = true, nullable = false)
private long boundId;

@Column
@Basic
private Timestamp startTimeStamp;

@Column
@Basic
private Timestamp endTimeStamp;

我创建了查询:

public interface BoundsDataRepository extends CrudRepository<Bound, Long> {
   @Query("from Bound b where b.startTimeStamp s <=:currentTimeStamp and b.endTimeStamp e>=:currentTimeStamp")
   List<Bound> findByCurrentTimeStamp(Timestamp currentTimeStamp);
}

我报错了。我应该如何命名此查询以及如何解决此问题?

感谢帮助!

我认为你的查询有问题:

  • 您不需要在列名中使用别名b.endTimeStamp e
  • 您使用了参数 :currentTimeStamp 但您没有在查询中传递任何参数

您的查询应如下所示:

@Query("from Bound b where b.startTimeStamp <= :currentTimeStamp and "
        + "b.endTimeStamp >= :currentTimeStamp")
List<Bound> findByCurrentTimeStamp(@Param("currentTimeStamp") Timestamp currentTimeStamp);