Enforce non-null value in requiredSingleResult/requiredUniqueResult

Closes gh-33300
This commit is contained in:
Juergen Hoeller
2024-08-05 15:53:17 +02:00
parent 1e804d8d4f
commit 28668d774b
2 changed files with 109 additions and 59 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -167,7 +167,11 @@ public abstract class DataAccessUtils {
if (results.size() > 1) {
throw new IncorrectResultSizeDataAccessException(1, results.size());
}
return results.iterator().next();
T result = results.iterator().next();
if (result == null) {
throw new TypeMismatchDataAccessException("Result value is null but no null value expected");
}
return result;
}
/**
@@ -235,7 +239,11 @@ public abstract class DataAccessUtils {
if (!CollectionUtils.hasUniqueObject(results)) {
throw new IncorrectResultSizeDataAccessException(1, results.size());
}
return results.iterator().next();
T result = results.iterator().next();
if (result == null) {
throw new TypeMismatchDataAccessException("Result value is null but no null value expected");
}
return result;
}
/**