i need to retrieve single row from table, and i was interested what approach is better.
On the one side getSingleResult is designed for retrieving single result, but it raises exception. Does this method have benefit in performance related to getResultList with
query.setFirstResult(0);
query.setMaxResults(1);
getSingleResultthrowsNonUniqueResultException, if there are multiple rows. It is designed to retrieve single result when there is truly a single result.The way you did is fine and JPA is designed to handle this properly. At the same time, you cannot compare it against
getSingleResultany way, since it won’t work.However, depend on the code you are working on, it is always better to refine the query to return single result, if that’s all what you want – then you can just call
getSingleResult.