diff --git a/spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java b/spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java index 9327c8cc9ff..58309754b55 100644 --- a/spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java +++ b/spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java @@ -116,7 +116,9 @@ public abstract class DataAccessUtils { * element has been found in the given Collection * @since 6.1 */ - public static Optional<@NonNull T> optionalResult(@Nullable Collection results) throws IncorrectResultSizeDataAccessException { + public static Optional<@NonNull T> optionalResult(@Nullable Collection results) + throws IncorrectResultSizeDataAccessException { + return Optional.ofNullable(singleResult(results)); } @@ -159,7 +161,9 @@ public abstract class DataAccessUtils { * @throws EmptyResultDataAccessException if no element at all * has been found in the given Collection */ - public static @NonNull T requiredSingleResult(@Nullable Collection results) throws IncorrectResultSizeDataAccessException { + public static @NonNull T requiredSingleResult(@Nullable Collection results) + throws IncorrectResultSizeDataAccessException { + if (CollectionUtils.isEmpty(results)) { throw new EmptyResultDataAccessException(1); } @@ -185,7 +189,9 @@ public abstract class DataAccessUtils { * has been found in the given Collection * @since 5.0.2 */ - public static T nullableSingleResult(@Nullable Collection results) throws IncorrectResultSizeDataAccessException { + public static T nullableSingleResult(@Nullable Collection results) + throws IncorrectResultSizeDataAccessException { + // This is identical to the requiredSingleResult implementation but differs in the // semantics of the incoming Collection (which we currently can't formally express) if (CollectionUtils.isEmpty(results)) {