Why NonUniqueResultException occurred in Spring JPA?

form dropdown in spring boot

NonUniqueResultException is an exception that is thrown by the javax.persistence.Query interface in the Java Persistence API (JPA) when a query that is expected to return a single result returns more than one result. This can occur when a SELECT query is executed that returns more than one row, or when a SELECT COUNT query returns a value greater than 1.

In Spring Data JPA, this exception can be thrown when using the JpaRepository interface to execute a query that is expected to return a single result, but actually returns multiple results. For example, the following code might throw a NonUniqueResultException:

User user = userRepository.findByUsername("john.doe");

If multiple users with the username “john.doe” exist in the database, this query will return more than one result, and a NonUniqueResultException will be thrown.

To avoid this exception, you can modify your query to ensure that it only returns a single result. For example, you might use a LIMIT clause in your SELECT query to limit the number of rows returned, or you might use a SELECT COUNT query to ensure that the result is always a single number.

Alternatively, you can catch the NonUniqueResultException and handle it appropriately in your code. For example, you might log the error and return a default value, or you might throw a custom exception to be handled by the calling code.

try {
    User user = userRepository.findByUsername("john.doe");
} catch (NonUniqueResultException e) {
    // Handle the exception here

How to Avoid NonUniqueResultException

There are several ways you can avoid NonUniqueResultException when using Spring Data JPA:

  1. Make sure your query is correctly constructed to return a single result. This might involve adding a LIMIT clause to your query, or using the SELECT clause to select a unique row based on some unique identifier.
  2. Make sure your database schema is correctly designed to enforce unique constraints where appropriate. This can help prevent multiple rows from being returned in the first place.
  3. Use the List interface instead of Query when running queries that might return multiple results. This will allow you to handle the possibility of multiple results gracefully, rather than causing an exception to be thrown.
  4. Use the findFirst method in your repository interface instead of findAll. This will return the first result of the query, or null if no results are found.
  5. Use the count method in your repository interface to determine the number of results that are returned by a query. If the count is greater than 1, you can either throw an exception or handle the multiple results in some other way.
  6. Use the List.size() method to check the number of results in the list returned by your query. If the size is greater than 1, you can either throw an exception or handle the multiple results in some other way.

Ultimately, the best approach will depend on the specific needs of your application and how you want to handle the possibility of multiple results being returned.


Please enter your comment!
Please enter your name here