Quick revision
The short answer
getSingleResult() demands exactly one row: NoResultException means no row matched, and NonUniqueResultException means more than one did. Seeing both in production tells us the code assumes a uniqueness the data does not have, so if the value should be unique we add a unique constraint and clean up the duplicates. If the result is genuinely optional, we use getResultStream().findFirst(), getSingleResultOrNull() in Jakarta Persistence 3.2, or a Spring Data method returning Optional; Spring translates the two exceptions into EmptyResultDataAccessException and IncorrectResultSizeDataAccessException.
What to remember
In this answer, we saw that getSingleResult() asserts exactly one result: NoResultException means no row matched and NonUniqueResultException means several did. Seeing both in production says that an empty result is a normal case the code does not handle, and that the database does not enforce a uniqueness the code relies on. We fixed the data and added a unique constraint, handled the optional case with getResultStream().findFirst(), getSingleResultOrNull() or a Spring Data Optional, and saw how Spring translates the exceptions into EmptyResultDataAccessException and IncorrectResultSizeDataAccessException.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
getSingleResult() demands exactly one row: NoResultException means no row matched, and NonUniqueResultException means more than one did. Seeing both in production tells us the code assumes a uniqueness the data does not have, so if the value should be unique we add a unique constraint and clean up the duplicates. If the result is genuinely optional, we use getResultStream().findFirst(), getSingleResultOrNull() in Jakarta Persistence 3.2, or a Spring Data method returning Optional; Spring translates the two exceptions into EmptyResultDataAccessException and IncorrectResultSizeDataAccessException.
Why this question is asked
These two exceptions look like noise in the logs, and the usual reaction is to wrap the call in a try-catch and move on. But together they are a clear message about the data and about an assumption in the code, and they deserve a real fix. Interviewers ask this because the answer shows whether we treat an exception as a symptom to silence or as evidence. In this answer, we will recap what getSingleResult() promises, reproduce both exceptions, see what they say about the data, fix the uniqueness with a constraint, and then write the optional lookup correctly in plain JPA and in Spring Data.
1What getSingleResult() Promises
A JPA query can be executed in several ways. getResultList() returns all matching results, possibly an empty list. getSingleResult() is stricter: it expects exactly one result. If there is none, it throws NoResultException. If there is more than one, it throws NonUniqueResultException. Both are runtime exceptions from jakarta.persistence, and by the JPA rules NoResultException does not mark the current transaction for rollback, while most other persistence exceptions do; check your provider if your code depends on this.
So the method is not only a way to get one object. It is an assertion: the code author is saying there will always be exactly one row.
2What the Two Exceptions Tell Us
NoResultException means the lookup sometimes finds nothing. That may be normal, for example a login with an email that is not registered, in which case the code should treat no result as a valid outcome rather than an exception. NonUniqueResultException means the data contains duplicates for a value the code believed unique. That is almost never normal. It usually means there is no unique constraint on the column, and two inserts with the same email got through, perhaps from a double-clicked sign-up form or two concurrent requests.
Seeing both in production tells us two separate things: the code does not handle an expected empty case, and the database does not enforce a rule the code relies on. Catching both exceptions and returning null would hide the second problem, and the duplicates would keep growing.
| Exception | Meaning | Right response |
|---|---|---|
NoResultException | no row matched | treat as optional if absence is normal |
NonUniqueResultException | two or more rows matched | fix the data and add a unique constraint |
EmptyResultDataAccessException | Spring's translation of no result | same as NoResultException |
IncorrectResultSizeDataAccessException | Spring's translation of too many | same as NonUniqueResultException |
3Making Uniqueness a Database Rule
If the value must be unique, the database should enforce it. Checking in the application first, with a select before the insert, does not work under concurrency, because two requests can both check, both find nothing and both insert. A unique constraint makes the second insert fail atomically. Before adding it, we merge or remove the existing duplicates, otherwise the constraint cannot be created.
If the email should be unique regardless of case, a unique index on lower(email) is the safer rule. Once the constraint exists, NonUniqueResultException cannot happen for this query, and the sign-up code can catch DataIntegrityViolationException to tell the user the email is taken.
4Handling an Optional Result in Plain JPA
For the empty case, we should not use exceptions for control flow. Exceptions are slow to create, clutter the logs and make the expected path look like an error. JPA gives us better options. getResultStream().findFirst() returns an Optional. Jakarta Persistence 3.2, implemented by Hibernate 7, adds getSingleResultOrNull(), which returns null for no result and still throws for more than one.
Notice a subtle difference. findFirst() silently picks one row if there are duplicates, so it hides the uniqueness problem. That is acceptable only after the unique constraint exists. Before that, getSingleResultOrNull() or a check on the list size is safer, because it still fails loudly on duplicates. If the query could match many rows, add setMaxResults(1) or an order by, so the database does not send rows we will throw away.
5How Spring Data Handles It
In Spring Data JPA, we usually do not call getSingleResult() ourselves. A repository method that returns Optional<Customer> or Customer returns Optional.empty() or null when nothing matches, and throws IncorrectResultSizeDataAccessException when more than one row matches. Spring's exception translation, which applies to repositories and @Repository beans, turns the JPA exceptions into Spring's DataAccessException hierarchy: NoResultException becomes EmptyResultDataAccessException, and NonUniqueResultException becomes IncorrectResultSizeDataAccessException.
The reason the translation matters is that callers can handle data access failures the same way whether the code underneath is JPA, JdbcTemplate or another technology. JdbcTemplate.queryForObject(), for example, throws the same EmptyResultDataAccessException when no row is found.
6Follow-up questions
Should we catch NoResultException and return null?
It works, but it uses an exception for a normal case and is easy to get wrong in a transaction. Prefer getResultStream().findFirst(), getSingleResultOrNull() on Jakarta Persistence 3.2, or a Spring Data method returning Optional, so the absence is part of the method's type.
Does NonUniqueResultException roll back the transaction?
By the JPA specification, NoResultException and NonUniqueResultException do not mark the transaction for rollback. But when the exception reaches a Spring @Transactional method boundary as a runtime exception, Spring rolls back by default. So in practice the transaction usually rolls back unless the exception is caught inside.
What if duplicates are allowed by design?
Then the query is asking the wrong question. Decide which row is wanted, the newest for example, and say so with order by and setMaxResults(1), or return a list and let the caller choose.
7Conclusion
In this answer, we saw that getSingleResult() asserts exactly one result: NoResultException means no row matched and NonUniqueResultException means several did. Seeing both in production says that an empty result is a normal case the code does not handle, and that the database does not enforce a uniqueness the code relies on. We fixed the data and added a unique constraint, handled the optional case with getResultStream().findFirst(), getSingleResultOrNull() or a Spring Data Optional, and saw how Spring translates the exceptions into EmptyResultDataAccessException and IncorrectResultSizeDataAccessException.
Open with
getSingleResult expects exactly one row. NoResultException means nothing matched, and NonUniqueResultException means two or more did. Seeing both in production tells me two things.
Then, if they want more
First, an empty result is a normal case, for example looking up an email that is not registered, and the code treats it as an exception instead of handling it. Second, there are duplicates for a value the code assumes is unique, which almost always means there is no unique constraint and concurrent inserts got through.
I would check with a group by having count greater than one query, merge the duplicates, and add a unique constraint, maybe on lower of email, because checking in the application before inserting does not work under concurrency.
For the optional case I would not catch exceptions; I would use getResultStream().findFirst() once the constraint exists, or getSingleResultOrNull in Jakarta Persistence 3.2, which still fails on duplicates, or in Spring Data simply a method returning Optional.
Spring Data returns empty for no match and throws IncorrectResultSizeDataAccessException for more than one, and Spring's exception translation turns NoResultException into EmptyResultDataAccessException and NonUniqueResultException into IncorrectResultSizeDataAccessException, so callers deal with one consistent hierarchy.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Hibernate 7
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "query.getSingleResult() sometimes throws NoResultException and sometimes...": https://scrivio-interview-library.netlify.app/databases/jpa-queries/13843adb9d/