Limiting resultset using @Query anotations

浪尽此生 提交于 2019-12-24 07:38:16

问题


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.

My repository code is:


回答1:


@Query("SELECT w FROM WardTransaction w WHERE w.visit = :visit ORDER BY w.admissionDateTime DESC")

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

Set Pageable object as it follows>

    Pageable pageSpecification = PageRequest(int page, int size)

Combination for Pageable and SimpleJpaRepository is the solution.

Take a look here

If you are using EntityManager and NamedQueries, there is a method setMaxResult that apply to Query object, but it is a different story.



来源:https://stackoverflow.com/questions/21923897/limiting-resultset-using-query-anotations

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!