Skip to content

Commit a66f675

Browse files
committed
Leniently accept null when calling delete(Specification).
Closes #2796
1 parent 0314115 commit a66f675

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@
1515
*/
1616
package org.springframework.data.jpa.repository.support;
1717

18-
import static org.springframework.data.jpa.repository.query.QueryUtils.COUNT_QUERY_STRING;
19-
import static org.springframework.data.jpa.repository.query.QueryUtils.DELETE_ALL_QUERY_BY_ID_STRING;
20-
import static org.springframework.data.jpa.repository.query.QueryUtils.DELETE_ALL_QUERY_STRING;
21-
import static org.springframework.data.jpa.repository.query.QueryUtils.applyAndBind;
22-
import static org.springframework.data.jpa.repository.query.QueryUtils.getQueryString;
23-
import static org.springframework.data.jpa.repository.query.QueryUtils.toOrders;
18+
import static org.springframework.data.jpa.repository.query.QueryUtils.*;
2419

2520
import jakarta.persistence.EntityManager;
2621
import jakarta.persistence.LockModeType;
@@ -524,10 +519,12 @@ public long delete(Specification<T> spec) {
524519
CriteriaBuilder builder = this.em.getCriteriaBuilder();
525520
CriteriaDelete<T> delete = builder.createCriteriaDelete(getDomainClass());
526521

527-
Predicate predicate = spec.toPredicate(delete.from(getDomainClass()), null, builder);
522+
if (spec != null) {
523+
Predicate predicate = spec.toPredicate(delete.from(getDomainClass()), null, builder);
528524

529-
if (predicate != null) {
530-
delete.where(predicate);
525+
if (predicate != null) {
526+
delete.where(predicate);
527+
}
531528
}
532529

533530
return this.em.createQuery(delete).executeUpdate();

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java

+26-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@
3333
import jakarta.persistence.criteria.Root;
3434
import lombok.Data;
3535

36-
import java.util.*;
36+
import java.util.ArrayList;
37+
import java.util.Collection;
38+
import java.util.Collections;
39+
import java.util.HashSet;
40+
import java.util.List;
41+
import java.util.Map;
42+
import java.util.Optional;
43+
import java.util.Set;
3744
import java.util.stream.Stream;
3845

3946
import org.assertj.core.api.SoftAssertions;
@@ -47,7 +54,14 @@
4754
import org.springframework.dao.DataIntegrityViolationException;
4855
import org.springframework.dao.IncorrectResultSizeDataAccessException;
4956
import org.springframework.dao.InvalidDataAccessApiUsageException;
50-
import org.springframework.data.domain.*;
57+
import org.springframework.data.domain.Example;
58+
import org.springframework.data.domain.ExampleMatcher;
59+
import org.springframework.data.domain.Page;
60+
import org.springframework.data.domain.PageImpl;
61+
import org.springframework.data.domain.PageRequest;
62+
import org.springframework.data.domain.Pageable;
63+
import org.springframework.data.domain.Slice;
64+
import org.springframework.data.domain.Sort;
5165
import org.springframework.data.domain.Sort.Direction;
5266
import org.springframework.data.domain.Sort.Order;
5367
import org.springframework.data.jpa.domain.Specification;
@@ -594,6 +608,16 @@ void returnsSamePageIfNoSpecGiven() {
594608
assertThat(repository.findAll((Specification<User>) null, pageable)).isEqualTo(repository.findAll(pageable));
595609
}
596610

611+
@Test // GH-2796
612+
void removesAllIfSpecificationIsNull() {
613+
614+
flushTestUsers();
615+
616+
repository.delete((Specification<User>) null);
617+
618+
assertThat(repository.count()).isEqualTo(0L);
619+
}
620+
597621
@Test
598622
void returnsAllAsPageIfNoPageableIsGiven() {
599623

0 commit comments

Comments
 (0)