Use a native SQL query by specifying a @NamedNativeQuery
annotation on the entity class, or by using the EntityManager.createNativeQuery
method. You will need to specify the type of the ResultSet using an appropriate class, or use a ResultSet mapping.
To use getSingleResult on a TypedQuery you can use
query.setFirstResult(0);
query.setMaxResults(1);
result = query.getSingleResult();
The easiest way is by using @Query with NativeQuery option like below:
@Query(value="SELECT 1 * FROM table ORDER BY anyField DESC LIMIT 1", nativeQuery = true)
Source: Stackoverflow.com