NonUniqueResultException ☕ Java

IncorrectResultSizeDataAccessException: query did not return a unique result: 2

A query expected one row but found several.

Seen on: Java

Meaning

Spring Data repository methods returning a single entity/Optional throw when multiple rows match. The data or the query assumption is wrong.

Common causes

  • Duplicate data where uniqueness was assumed
  • findBy... returning single instead of List
  • Missing unique constraint

⚡ Quick fix

  1. Return a List and handle duplicates
  2. Add a unique constraint
  3. Use findFirstBy… with ordering when you want one

Detailed fix by platform

Java

  1. Optional<User> findFirstByEmailOrderByIdDesc(String email);

How to diagnose

  1. Query — Which method?
  2. Data — Why several rows?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.