EDIT: Solutions to this problem are provided in the second and fourth answer regarding this question setMaxResults for Spring-Data-JPA annotation?
Goal: Fetch the largest/smallest element by property z using a Spring Data JPA repository and the Spring Query annotation.
What I have so far
@Query("SELECT xelement FROM x xelement ORDER BY xelement.z")
public List<X> findFirstElement();
Problem: This query fetches all elements (which is not really effective). If I would use the EntityManager direcly, I could set the number of results using
entityManager.setMaxResults(1)
to only get the first element.
Question: How do I specify the maximum number of results using the @Query annotation?
Idea: Is using a PageRequest of size 0 the way to go?
Constraints: I am aware of the "FindFirstBy...." query feature but I want/have to use the @Query
annotation.