使用@Query注释限制结果集 [英] Limiting resultset using @Query anotations
问题描述
我有一个查询,希望限制结果集的大小.由于JPA不支持"LIMIT",因此我一直在尝试找出正确的方法.我已经尝试过:
I have a query where I want to limit the size of the resultset. As JPA does not support 'LIMIT', I have been trying to work out the correct approach. I have tried:
@Query("SELECT w FROM WardTransaction w WHERE w.visit = :visit ORDER BY w.admissionDateTime DESC").setMaxResults(1)
public WardTransaction findCurrent(@Param("visit") Visit visit);
这是不正确的.我只是在寻找有关正确语法的指导.
Which is not correct. I am just looking for some guidance as to the correct syntax.
我的存储库代码为:
推荐答案
@Query("SELECT w FROM WardTransaction w WHERE w.visit = :visit ORDER BY w.admissionDateTime DESC")
上面是查询方法的定义,要在您的查询中设置MaxMaxResult,您需要使用如下的Pageable对象.
The above is the query method definition, to setMaxResult in your query you need to use Pageable object as it follows.
@Query("SELECT w FROM WardTransaction w WHERE w.visit = :visit ORDER BY w.admissionDateTime DESC")
public void List<Entities> findAll(Pageable pageable)
JPA repository must implement SimpleJpaRepository
按如下所示设置Pageable对象>
Set Pageable object as it follows>
Pageable pageSpecification = PageRequest(int page, int size)
Pageable和SimpleJpaRepository的组合是解决方案.
Combination for Pageable and SimpleJpaRepository is the solution.
如果您使用的是EntityManager和NamedQueries,则有一个方法setMaxResult适用于Query对象,但这是另一回事.
If you are using EntityManager and NamedQueries, there is a method setMaxResult that apply to Query object, but it is a different story.
这篇关于使用@Query注释限制结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!