From 3da1739cbee6435d316e8d2ce24fc21e1984effd Mon Sep 17 00:00:00 2001 From: Gavin King Date: Thu, 18 Dec 2025 12:52:31 +0100 Subject: [PATCH] HHH-20006 uniqueElement() did not have a self-consistent definition of "same" Really, getSingleResult() is saying there should be a single element in getResultList(). If that's not the case, you should use 'select distinct'. That said, we might want a way to recover the old behavior (H5, apparently) of making things distinct in memory according to equals(). Though, actually, I believe you can just use a ResultListTransformer for that. Perhaps we should just add a built-in one for migration purposes. --- .../query/spi/AbstractSelectionQuery.java | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/spi/AbstractSelectionQuery.java b/hibernate-core/src/main/java/org/hibernate/query/spi/AbstractSelectionQuery.java index 2238e267c70c..14b002deeb3d 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/spi/AbstractSelectionQuery.java +++ b/hibernate-core/src/main/java/org/hibernate/query/spi/AbstractSelectionQuery.java @@ -304,19 +304,11 @@ public R getSingleResult() { protected static T uniqueElement(List list) throws NonUniqueResultException { final int size = list.size(); - if ( size == 0 ) { - return null; - } - else { - final T first = list.get( 0 ); - // todo (6.0) : add a setting here to control whether to perform this validation or not - for ( int i = 1; i < size; i++ ) { - if ( list.get( i ) != first ) { - throw new NonUniqueResultException( list.size() ); - } - } - return first; - } + return switch ( size ) { + case 0 -> null; + case 1 -> list.get( 0 ); + default -> throw new NonUniqueResultException( size ); + }; } @Override