Spring 数据 JPA 中的按日期排序限制
Order By Date Desc Limit in Spring Data JPA
我正在尝试使用限制查询来限制查询结果。没有限制,查询按预期工作。
@Query("SELECT a FROM DrmAdpodTimeSlot a where a.startTime > :startTime order by a.startTime desc")
public List<DrmAdpodTimeSlot> findByStartTime(@Param("startTime") Timestamp startTime);
但是当我尝试使用limit(no.of条记录)来限制记录时,如下,
@Query("SELECT a FROM DrmAdpodTimeSlot a where a.startTime > :startTime order by a.startTime desc limit 2")
public List<DrmAdpodTimeSlot> findByStartTime(@Param("startTime") Timestamp startTime);
从上面的查询中我得到以下错误,
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: limit near line 1, column 110 [SELECT a FROM com.dooreme.domain.DrmAd
podTimeSlot a where a.startTime > :startTime order by a.startTime desc limit 2]
如何在spring数据jpa查询中使用order by limit查询?
您不能向 Query
注释添加分页支持。当您使用 Spring Data JPA 时,无需向 HQL/JPQL
添加排序和分页功能。使用 Pageable
作为第二个参数,如下所示:
@Query("SELECT a FROM DrmAdpodTimeSlot a where a.startTime > :startTime")
public List<DrmAdpodTimeSlot> findByStartTime(@Param("startTime") Timestamp startTime, Pageable pageable);
Pageable
封装了排序和分页功能,如spring data jpa doc所说:
Add Pageable
instance to the query method to dynamically add paging to
your statically defined query. A Page
knows about the total number of
elements and pages available. It does so by the infrastructure
triggering a count query to calculate the overall number. As this
might be expensive depending on the store used, Slice
can be used as
return instead. A Slice
only knows about whether there’s a next Slice
available which might be just sufficient when walking thought a larger
result set.
因此,您可以使用:
@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);
还有:
Sorting options are handled through the Pageable instance too.
None 的其他答案确实回答了您关于如何限制前 2 个开始时间降序的问题。我不确定如何使用 jpql 执行此操作,但使用 jpa 查询,您可以执行 findTop2ByOrderByStartTimeDesc
另请参阅此 post
How would I write SELECT TOP 25 sql query in Spring data repository
您可以在存储库中使用您的查询:
@Query("SELECT a FROM DrmAdpodTimeSlot a WHERE a.startTime>'?1' ORDER BY a.startTime DESC")
List<DrmAdpodTimeSlot> findByStartTime(String startTime, Pageable pageable);
}
并且在服务中,使用 PageRequest,返回一个页面对象:
Page<DrmAdpodTimeSlot> top2 = arcustRepository.findByStartTime(arcustno, PageRequest.of(0, 2));
List<DrmAdpodTimeSlot> top2StartTimes = top2.getContent();
我正在尝试使用限制查询来限制查询结果。没有限制,查询按预期工作。
@Query("SELECT a FROM DrmAdpodTimeSlot a where a.startTime > :startTime order by a.startTime desc")
public List<DrmAdpodTimeSlot> findByStartTime(@Param("startTime") Timestamp startTime);
但是当我尝试使用limit(no.of条记录)来限制记录时,如下,
@Query("SELECT a FROM DrmAdpodTimeSlot a where a.startTime > :startTime order by a.startTime desc limit 2")
public List<DrmAdpodTimeSlot> findByStartTime(@Param("startTime") Timestamp startTime);
从上面的查询中我得到以下错误,
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: limit near line 1, column 110 [SELECT a FROM com.dooreme.domain.DrmAd
podTimeSlot a where a.startTime > :startTime order by a.startTime desc limit 2]
如何在spring数据jpa查询中使用order by limit查询?
您不能向 Query
注释添加分页支持。当您使用 Spring Data JPA 时,无需向 HQL/JPQL
添加排序和分页功能。使用 Pageable
作为第二个参数,如下所示:
@Query("SELECT a FROM DrmAdpodTimeSlot a where a.startTime > :startTime")
public List<DrmAdpodTimeSlot> findByStartTime(@Param("startTime") Timestamp startTime, Pageable pageable);
Pageable
封装了排序和分页功能,如spring data jpa doc所说:
Add
Pageable
instance to the query method to dynamically add paging to your statically defined query. APage
knows about the total number of elements and pages available. It does so by the infrastructure triggering a count query to calculate the overall number. As this might be expensive depending on the store used,Slice
can be used as return instead. ASlice
only knows about whether there’s a nextSlice
available which might be just sufficient when walking thought a larger result set.
因此,您可以使用:
@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);
还有:
Sorting options are handled through the Pageable instance too.
None 的其他答案确实回答了您关于如何限制前 2 个开始时间降序的问题。我不确定如何使用 jpql 执行此操作,但使用 jpa 查询,您可以执行 findTop2ByOrderByStartTimeDesc
另请参阅此 post How would I write SELECT TOP 25 sql query in Spring data repository 您可以在存储库中使用您的查询:
@Query("SELECT a FROM DrmAdpodTimeSlot a WHERE a.startTime>'?1' ORDER BY a.startTime DESC")
List<DrmAdpodTimeSlot> findByStartTime(String startTime, Pageable pageable);
}
并且在服务中,使用 PageRequest,返回一个页面对象:
Page<DrmAdpodTimeSlot> top2 = arcustRepository.findByStartTime(arcustno, PageRequest.of(0, 2));
List<DrmAdpodTimeSlot> top2StartTimes = top2.getContent();