Spring数据JPA中的订单日期描述限制

Spring数据JPA中的订单日期描述限制,spring,hibernate,jpa,spring-data,spring-data-jpa,Spring,Hibernate,Jpa,Spring Data,Spring Data Jpa,我正在尝试使用limit query来限制查询结果。如果超出限制,查询将按预期工作 @Query("SELECT a FROM DrmAdpodTimeSlot a where a.startTime > :startTime order by a.startTime desc") public List<DrmAdpodTimeSlot> findByStartTime(@Param("startTime") Timestamp startTime); 如何在spr

我正在尝试使用limit query来限制查询结果。如果超出限制,查询将按预期工作

@Query("SELECT a FROM DrmAdpodTimeSlot a where a.startTime > :startTime order by a.startTime desc")
    public List<DrmAdpodTimeSlot> findByStartTime(@Param("startTime") Timestamp startTime);

如何在spring data jpa查询中使用按限制排序查询?

您不能向
查询
注释添加分页支持。当您使用Spring数据JPA时,不需要将排序和分页功能添加到
HQL/JPQL
。使用
Pageable
作为第二个参数,如下所示:

@Query("SELECT a FROM DrmAdpodTimeSlot a where a.startTime > :startTime")
public List<DrmAdpodTimeSlot> findByStartTime(@Param("startTime") Timestamp startTime, Pageable pageable);
或:

@Query(“从DrmAdpodTimeSlot a中选择a,其中a.startTime>:startTime”)
公共切片findByStartTime(@Param(“startTime”)时间戳startTime,可分页;
此外:

排序选项也通过Pageable实例处理


其他答案都没有真正回答您的问题,即如何限制前2个开始时间的降序。我不知道如何使用jpql来实现,但是使用jpa查询,您可以执行
findTop2ByOrderByStartTimeDesc

还有,请看这篇文章 您可以在查询中使用存储库:

@Query("SELECT a FROM DrmAdpodTimeSlot a WHERE a.startTime>'?1' ORDER BY a.startTime DESC")
    List<DrmAdpodTimeSlot> findByStartTime(String startTime, Pageable pageable);
}
@Query(“从DrmAdpodTimeSlot a中选择a,其中a.startTime>”?1“按a.startTime DESC排序”)
列出findByStartTime(字符串开始时间,可分页);
}
在服务中,使用PageRequest返回页面对象:

Page<DrmAdpodTimeSlot> top2 = arcustRepository.findByStartTime(arcustno, PageRequest.of(0, 2));
List<DrmAdpodTimeSlot> top2StartTimes = top2.getContent();
Page top2=arcustRepository.findByStartTime(arcustno,PageRequest.of(0,2));
List top2StartTimes=top2.getContent();

没有JPQL引用会显示“LIMIT”,因为它不是有效的语法。阅读文档!JPQL不支持分页。。。但这是在JPA查询对象上使用setMaxResults/setFirstResult的过程。我的错误,我的答案是在
@Query
注释的上下文中,我将修复我的错误answer@NeilStockton-这不是分页,而是限制和偏移。而且,JPQL不支持这一点,查询API支持。实际上,返回
Slice
的方法每次调用只执行一个DB查询
@Query("SELECT a FROM DrmAdpodTimeSlot a where a.startTime > :startTime")
public Page<DrmAdpodTimeSlot> findByStartTime(@Param("startTime") Timestamp startTime, Pageable pageable);
@Query("SELECT a FROM DrmAdpodTimeSlot a where a.startTime > :startTime")
public Slice<DrmAdpodTimeSlot> findByStartTime(@Param("startTime") Timestamp startTime, Pageable pageable);
@Query("SELECT a FROM DrmAdpodTimeSlot a WHERE a.startTime>'?1' ORDER BY a.startTime DESC")
    List<DrmAdpodTimeSlot> findByStartTime(String startTime, Pageable pageable);
}
Page<DrmAdpodTimeSlot> top2 = arcustRepository.findByStartTime(arcustno, PageRequest.of(0, 2));
List<DrmAdpodTimeSlot> top2StartTimes = top2.getContent();