hibernate/hibernate-orm · error · NonUniqueResultException

Query did not return a unique result: " + resultCount + "…

Error message

Query did not return a unique result: " + resultCount + " results were returned

What it means

Raised (as NonUniqueResultException) by the unique-result transformer returned from ResultListTransformer.uniqueResultTransformer() when the query result list contains multiple non-equivalent rows. It exists to mimic uniqueResult() semantics on list-based APIs: rows are compared pairwise, and any genuinely distinct second row triggers the exception. The offending input is the multi-element result list produced by the executed query.

Solutions

  1. Add restricting criteria so the query returns at most one row.
  2. Use getResultList() if multiple results are valid.
Defensive patterns

Strategy: validation

When it happens

Trigger: A runtime precondition of the Hibernate API is violated.

Common situations: Occurs when application code or configuration does not satisfy the documented contract of the API being called.


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/efdd472a33616e0e. Report an issue: GitHub.

Appendix: source

Thrown at hibernate-core/src/main/java/org/hibernate/query/ResultListTransformer.java:68

	 * {@link NonUniqueResultException} otherwise.
	 *
	 * @param <T> The result list element type
	 *
	 * @since 7.3
	 */
	static <T> ResultListTransformer<T> uniqueResultTransformer() {
		return resultList ->
				switch ( resultList.size() ) {
					case 0, 1 -> resultList;
					default -> {
						final var first = resultList.get( 0 );
						for ( int i = 1; i < resultList.size(); i++ ) {
							final T current = resultList.get( i );
							if ( !Objects.equals( first, current )
									// also consider case of multiple select items in an Object[]
									&& !( first instanceof Object[] array
											&& !Arrays.equals( array, (Object[]) current ) ) ) {
								throw new NonUniqueResultException( resultList.size() );
							}
						}
						yield List.of( first );
					}
				};
	}
}

View on GitHub (pinned to fad1729dce)