1

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:

skyman
  • 2,255
  • 4
  • 32
  • 55

1 Answers1

3
@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.

Koitoer
  • 18,778
  • 7
  • 63
  • 86
  • Actually, I have a repository class - I am not sure if this helps? I will have a look at the reference.. – skyman Feb 21 '14 at 06:15
  • Yes, you are applying the @Query above the method,try to change with my recomendation of SimpleJpa and Pageable – Koitoer Feb 21 '14 at 06:38