使用spring数据jpa@query注释查找最大查询结果
可以使用.setMaxResults(1).getResultList()和@query限制查询结果-类似于:使用spring数据jpa@query注释查找最大查询结果,jpa,spring-data-jpa,Jpa,Spring Data Jpa,可以使用.setMaxResults(1).getResultList()和@query限制查询结果-类似于: @Query("SELECT l FROM LocationLog l WHERE l.visit = :visit ORDER BY l.eventTime").setMaxResults(1).getResultList() public LocationLog findLast(@Param("visit") Visit visit); 此代码不正确,因为setMaxResul
@Query("SELECT l FROM LocationLog l WHERE l.visit = :visit ORDER BY l.eventTime").setMaxResults(1).getResultList()
public LocationLog findLast(@Param("visit") Visit visit);
此代码不正确,因为setMaxResults等不在@Query?l中。您不能在
@Query
注释中设置分页选项,但可以通过其他方式进行设置。您可以将方法声明更改为
public LocationLog find(@Param("visit") Visit visit, Pageable pageable);
然后,您将能够向查询调用传递其他参数:
Pageable firstFive = new PageRequest(0, 5);
LocationLog locLog = locationLogDao.find(visit, firstFive);
还可以选择了解查询结果的当前上下文,您可以使用Page
接口将方法返回类型更改为Page
。你可以找到所有必要的信息