Return value of JPA query when no matches found
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In JPA, the return value for "no matches" depends on which query method you call. The two most important cases are getSingleResult(), which throws an exception when nothing is found, and getResultList(), which returns an empty list.
getSingleResult() does not return null
This is the behavior that surprises many people. If you ask JPA for exactly one result and there is none, JPA throws NoResultException.
It also throws NonUniqueResultException if more than one row matches. So getSingleResult() is not "return one or return null." It is "return exactly one or fail."
getResultList() returns an empty list
If you want a no-match case that is easy to handle without exceptions, use getResultList().
This is often the cleaner choice when zero, one, or many results are all acceptable outcomes.
Choose the API based on semantics
If your domain logic truly requires exactly one match, getSingleResult() expresses that intent. If zero results are normal and not exceptional, getResultList() is usually the better fit.
That distinction matters because exception-driven control flow is awkward when "not found" is part of normal application behavior. A list-based or optional-style API is usually easier to read in those cases.
Repository-style code often wraps this behavior
In higher-level frameworks built on JPA, the raw JPA behavior may be wrapped into more ergonomic return types such as Optional<T> or collection-returning repository methods. But underneath, the same question still exists: is the absence of data exceptional, or is it an expected outcome.
The cleanest code makes that choice explicit in the method signature instead of forcing callers to guess.
Repository methods often make the contract clearer
If your application uses repository abstractions above raw JPA, a method returning Optional<User> communicates the single-or-none case much better than manual exception handling around getSingleResult(). A method returning List<User> communicates zero-or-many just as clearly. Even though the underlying persistence behavior still matters, the higher-level API can make the no-match case easier to reason about.
Do not hide semantics with ad hoc null handling
A common anti-pattern is wrapping JPA query code and converting every no-result situation into null without making that contract explicit. That removes information from the API and makes callers guess whether null means no row, query bug, or some higher-level mapping issue. Clear return semantics are better than a vague sentinel value.
Common Pitfalls
- Expecting
getSingleResult()to returnnullwhen no rows match. - Using
getSingleResult()when zero matches are normal business behavior. - Forgetting that
getSingleResult()can also fail when more than one row matches. - Catching broad exceptions instead of handling
NoResultExceptiondeliberately. - Using list-returning queries but forgetting to check
isEmpty()before reading the first element.
Summary
- '
getSingleResult()throwsNoResultExceptionwhen nothing matches.' - '
getResultList()returns an empty list when nothing matches.' - Use the single-result API only when exactly one row is the intended contract.
- Prefer list or optional-style flows when "not found" is a normal case.
- In JPA, the correct no-match handling depends on the query method, not on one universal return rule.
Related reading
- Returning from a finally block in Java
- Returning JSON object as response in Spring Boot
- Returning null as an int permitted with ternary operator but not if statement
- Returning value from Thread
- Reverse a string in Java
- RMI alternatives for bidirectional asynchronous calls and callbacks through firewalls or NAT
- rmi registry binding issue
- Role/Purpose of ContextLoaderListener in Spring?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.