Select the top row in JPQL without using native query option

Tags: , ,



How is it possible to select the first row at the top of the selection without using native query option in JPQL/JPA?

@Query("select e from FOO e order by e.orderNumber desc")

Answer

You might be able to use a max subquery here to restrict to the “first” row:

select e from FOO e where orderNumber = (select max(f.orderNumber) from FOO f);

This would be logically correct if orderNumber would always be guaranteed to be unique, in which case there would only be one max value.



Source: stackoverflow