Reputation: 2335
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:
Upvotes: 1
Views: 1374
Reputation: 19533
@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.
Upvotes: 3