Skip to content

Commit c94259c

Browse files
SWQXDBAmp911de
authored andcommitted
Fix handling of null predicate in Specification.not().
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. Closes #3849 Original pull request: #3856 Signed-off-by: SWQXDBA <[email protected]>
1 parent 9fab81a commit c94259c

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ static <T> Specification<T> not(Specification<T> spec) {
156156
return (root, query, builder) -> {
157157

158158
Predicate predicate = spec.toPredicate(root, query, builder);
159-
return predicate != null ? builder.not(predicate) : null;
159+
return predicate != null ? builder.not(predicate) : builder.disjunction();
160160
};
161161
}
162162

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,17 @@ void orCombinesSpecificationsInOrder() {
139139
verify(builder).or(firstPredicate, secondPredicate);
140140
}
141141

142+
@Test // #3849
143+
void notWithNullPredicate() {
144+
Specification<Object> spec = (r, q, cb) -> null;
145+
146+
Specification<Object> notSpec = Specification.not(spec);
147+
148+
notSpec.toPredicate(root, query, builder);
149+
150+
verify(builder).disjunction();
151+
}
152+
142153
static class SerializableSpecification implements Serializable, Specification<Object> {
143154

144155
@Override

0 commit comments

Comments
 (0)