Skip to content

Commit 41bfd41

Browse files
committed
Polishing.
Add fix to Update, Delete, and PredicateSpecification. Reformat code. Refine tests. See #3849 Original pull request: #3856
1 parent dc854df commit 41bfd41

File tree

6 files changed

+42
-9
lines changed

6 files changed

+42
-9
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
import java.util.Arrays;
2525
import java.util.stream.StreamSupport;
2626

27-
import org.springframework.lang.CheckReturnValue;
28-
2927
import org.jspecify.annotations.Nullable;
28+
29+
import org.springframework.lang.CheckReturnValue;
3030
import org.springframework.lang.Contract;
3131
import org.springframework.util.Assert;
3232

@@ -159,7 +159,7 @@ static <T> DeleteSpecification<T> not(DeleteSpecification<T> spec) {
159159
return (root, delete, builder) -> {
160160

161161
Predicate predicate = spec.toPredicate(root, delete, builder);
162-
return predicate != null ? builder.not(predicate) : null;
162+
return predicate != null ? builder.not(predicate) : builder.disjunction();
163163
};
164164
}
165165

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
import java.util.Arrays;
2424
import java.util.stream.StreamSupport;
2525

26-
import org.springframework.lang.CheckReturnValue;
27-
2826
import org.jspecify.annotations.Nullable;
27+
28+
import org.springframework.lang.CheckReturnValue;
2929
import org.springframework.lang.Contract;
3030
import org.springframework.util.Assert;
3131

@@ -113,7 +113,7 @@ static <T> PredicateSpecification<T> not(PredicateSpecification<T> spec) {
113113
return (root, builder) -> {
114114

115115
Predicate predicate = spec.toPredicate(root, builder);
116-
return predicate != null ? builder.not(predicate) : null;
116+
return predicate != null ? builder.not(predicate) : builder.disjunction();
117117
};
118118
}
119119

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
import java.util.Arrays;
2525
import java.util.stream.StreamSupport;
2626

27-
import org.springframework.lang.CheckReturnValue;
28-
2927
import org.jspecify.annotations.Nullable;
28+
29+
import org.springframework.lang.CheckReturnValue;
3030
import org.springframework.lang.Contract;
3131
import org.springframework.util.Assert;
3232

@@ -180,7 +180,7 @@ static <T> UpdateSpecification<T> not(UpdateSpecification<T> spec) {
180180
return (root, update, builder) -> {
181181

182182
Predicate predicate = spec.toPredicate(root, update, builder);
183-
return predicate != null ? builder.not(predicate) : null;
183+
return predicate != null ? builder.not(predicate) : builder.disjunction();
184184
};
185185
}
186186

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

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

163+
@Test // GH-3849
164+
void notWithNullPredicate() {
165+
166+
when(builder.disjunction()).thenReturn(mock(Predicate.class));
167+
168+
DeleteSpecification<Object> notSpec = DeleteSpecification.not((r, q, cb) -> null);
169+
170+
assertThat(notSpec.toPredicate(root, delete, builder)).isNotNull();
171+
verify(builder).disjunction();
172+
}
173+
163174
static class SerializableSpecification implements Serializable, DeleteSpecification<Object> {
164175

165176
@Override

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

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

161+
@Test // GH-3849
162+
void notWithNullPredicate() {
163+
164+
when(builder.disjunction()).thenReturn(mock(Predicate.class));
165+
166+
PredicateSpecification<Object> notSpec = PredicateSpecification.not((r, cb) -> null);
167+
168+
assertThat(notSpec.toPredicate(root, builder)).isNotNull();
169+
verify(builder).disjunction();
170+
}
171+
161172
static class SerializableSpecification implements Serializable, PredicateSpecification<Object> {
162173

163174
@Override

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

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

163+
@Test // GH-3849
164+
void notWithNullPredicate() {
165+
166+
when(builder.disjunction()).thenReturn(mock(Predicate.class));
167+
168+
UpdateSpecification<Object> notSpec = UpdateSpecification.not((r, q, cb) -> null);
169+
170+
assertThat(notSpec.toPredicate(root, update, builder)).isNotNull();
171+
verify(builder).disjunction();
172+
}
173+
163174
static class SerializableSpecification implements Serializable, UpdateSpecification<Object> {
164175

165176
@Override

0 commit comments

Comments
 (0)