Skip to content

Commit 964448e

Browse files
committed
Fix handling of null predicate in Specification.not()
spring-projects#3849 When toPredicate() returns null, Specification.not() now returns builder.disjunction() instead of builder.not(null). This change ensures proper handling of null predicates in negated specifications.
1 parent 6ac5536 commit 964448e

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/domain/Specification.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ static <T> Specification<T> not(@Nullable Specification<T> spec) {
5656

5757
return spec == null //
5858
? (root, query, builder) -> null //
59-
: (root, query, builder) -> builder.not(spec.toPredicate(root, query, builder));
59+
: (root, query, builder) -> {
60+
Predicate predicate = spec.toPredicate(root, query, builder);
61+
if(predicate != null) {
62+
return builder.not(predicate);
63+
}
64+
return builder.disjunction();
65+
};
6066
}
6167

6268
/**

spring-data-jpa/src/test/java/org/springframework/data/jpa/domain/SpecificationUnitTests.java

+11
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,17 @@ void orCombinesSpecificationsInOrder() {
214214
verify(builder).or(firstPredicate, secondPredicate);
215215
}
216216

217+
@Test // #3849
218+
void notWithNullPredicate() {
219+
Specification<Object> spec = (r, q, cb) -> null;
220+
221+
Specification<Object> notSpec = Specification.not(spec);
222+
223+
notSpec.toPredicate(root, query, builder);
224+
225+
verify(builder).disjunction();
226+
}
227+
217228
static class SerializableSpecification implements Serializable, Specification<Object> {
218229

219230
@Override

0 commit comments

Comments
 (0)