Allow empty Iterable arguments in JdbcAggregateTemplate again.

This also affects repositories since they delegate to the template.

Closes #1401
This commit is contained in:
Jens Schauder
2023-01-03 09:34:15 +01:00
parent 5d3e737393
commit 1ff403e174
2 changed files with 27 additions and 3 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.data.jdbc.core;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
@@ -170,7 +171,11 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
@Override
public <T> Iterable<T> saveAll(Iterable<T> instances) {
Assert.isTrue(instances.iterator().hasNext(), "Aggregate instances must not be empty");
Assert.notNull(instances, "Aggregate instances must not be null");
if (!instances.iterator().hasNext()) {
return Collections.emptyList();
}
return performSaveAll(instances);
}
@@ -327,7 +332,9 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
@Override
public <T> void deleteAllById(Iterable<?> ids, Class<T> domainType) {
Assert.isTrue(ids.iterator().hasNext(), "Ids must not be empty");
if (!ids.iterator().hasNext()) {
return;
}
BatchingAggregateChange<T, DeleteAggregateChange<T>> batchingAggregateChange = BatchingAggregateChange
.forDelete(domainType);
@@ -356,7 +363,9 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
@Override
public <T> void deleteAll(Iterable<? extends T> instances) {
Assert.isTrue(instances.iterator().hasNext(), "Aggregate instances must not be empty");
if (!instances.iterator().hasNext()) {
return;
}
Map<Class, List<Object>> groupedByType = new HashMap<>();

View File

@@ -312,6 +312,21 @@ public class JdbcAggregateTemplateUnitTests {
assertThat(all).containsExactly(alfred2, neumann2);
}
@Test // GH-1401
public void saveAllWithEmptyListDoesNothing() {
assertThat(template.saveAll(emptyList())).isEmpty();
}
@Test // GH-1401
public void deleteAllWithEmptyListDoesNothing() {
template.deleteAll(emptyList());
}
@Test // GH-1401
public void deleteAllByIdWithEmptyListDoesNothing() {
template.deleteAllById(emptyList(), SampleEntity.class);
}
@Data
@AllArgsConstructor
private static class SampleEntity {