From 6cf36acd5352c6b2b5968c10327c79b888cd9a04 Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Thu, 21 Nov 2019 20:25:42 +0100 Subject: [PATCH] Optimize AbstractProductQuery#getSingleResult Also brings the method in line with the spec for some in-memory databases like HSQL. --- .../query/internal/AbstractProducedQuery.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/internal/AbstractProducedQuery.java b/hibernate-core/src/main/java/org/hibernate/query/internal/AbstractProducedQuery.java index 76dd6f77be..15e69a7104 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/internal/AbstractProducedQuery.java +++ b/hibernate-core/src/main/java/org/hibernate/query/internal/AbstractProducedQuery.java @@ -1572,10 +1572,16 @@ public abstract class AbstractProducedQuery implements QueryImplementor { public R getSingleResult() { try { final List list = list(); - if ( list.size() == 0 ) { - throw new NoResultException( "No entity found for query" ); + switch ( list.size() ) { + case 0: + throw new NoResultException( "No entity found for query" ); + + case 1: + return list.get( 0 ); + + default: + throw new NonUniqueResultException( list.size() ); } - return uniqueElement( list ); } catch ( HibernateException e ) { throw getExceptionConverter().convert( e, getLockOptions() );