Leniently accept null when calling delete(Specification).

Closes #2796
This commit is contained in:
Mark Paluch
2023-03-21 11:35:53 +01:00
parent 0314115c39
commit a66f67506a
2 changed files with 32 additions and 11 deletions

View File

@@ -15,12 +15,7 @@
*/
package org.springframework.data.jpa.repository.support;
import static org.springframework.data.jpa.repository.query.QueryUtils.COUNT_QUERY_STRING;
import static org.springframework.data.jpa.repository.query.QueryUtils.DELETE_ALL_QUERY_BY_ID_STRING;
import static org.springframework.data.jpa.repository.query.QueryUtils.DELETE_ALL_QUERY_STRING;
import static org.springframework.data.jpa.repository.query.QueryUtils.applyAndBind;
import static org.springframework.data.jpa.repository.query.QueryUtils.getQueryString;
import static org.springframework.data.jpa.repository.query.QueryUtils.toOrders;
import static org.springframework.data.jpa.repository.query.QueryUtils.*;
import jakarta.persistence.EntityManager;
import jakarta.persistence.LockModeType;
@@ -524,10 +519,12 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
CriteriaBuilder builder = this.em.getCriteriaBuilder();
CriteriaDelete<T> delete = builder.createCriteriaDelete(getDomainClass());
Predicate predicate = spec.toPredicate(delete.from(getDomainClass()), null, builder);
if (spec != null) {
Predicate predicate = spec.toPredicate(delete.from(getDomainClass()), null, builder);
if (predicate != null) {
delete.where(predicate);
if (predicate != null) {
delete.where(predicate);
}
}
return this.em.createQuery(delete).executeUpdate();

View File

@@ -33,7 +33,14 @@ import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import lombok.Data;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import org.assertj.core.api.SoftAssertions;
@@ -47,7 +54,14 @@ import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.*;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;
@@ -594,6 +608,16 @@ public class UserRepositoryTests {
assertThat(repository.findAll((Specification<User>) null, pageable)).isEqualTo(repository.findAll(pageable));
}
@Test // GH-2796
void removesAllIfSpecificationIsNull() {
flushTestUsers();
repository.delete((Specification<User>) null);
assertThat(repository.count()).isEqualTo(0L);
}
@Test
void returnsAllAsPageIfNoPageableIsGiven() {