skyman
skyman

Reputation: 2335

Finding Maximum query result using spring-data-jpa @Query annotation

Is is possible to limit query results using .setMaxResults(1).getResultList() and @Query - something like:

@Query("SELECT l FROM LocationLog l WHERE l.visit = :visit ORDER BY l.eventTime").setMaxResults(1).getResultList()
public LocationLog findLast(@Param("visit") Visit visit);

This code is not correct as the setMaxResults and so on is outside the @Query?l

Upvotes: 0

Views: 4119

Answers (1)

Maciej Dobrowolski
Maciej Dobrowolski

Reputation: 12140

You cannot set pagination options in @Query annotation, but it can be done in other way. You can change your method declaration to

public LocationLog find(@Param("visit") Visit visit, Pageable pageable);

Then you will be able to pass additional argument to query invocation:

Pageable firstFive = new PageRequest(0, 5);
LocationLog locLog = locationLogDao.find(visit, firstFive);

There's also option to know current context of the query results you can use Page interface changing method return type to Page<LocationLog>. You can find all necessary informations here.

Upvotes: 1

Related Questions