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 3045225207
commit e325c0152b
2 changed files with 44 additions and 5 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;
@@ -171,7 +172,10 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
public <T> Iterable<T> saveAll(Iterable<T> instances) {
Assert.notNull(instances, "Aggregate instances must not be null");
Assert.isTrue(instances.iterator().hasNext(), "Aggregate instances must not be empty");
if (!instances.iterator().hasNext()) {
return Collections.emptyList();
}
List<EntityAndChangeCreator<T>> entityAndChangeCreators = new ArrayList<>();
for (T instance : instances) {
@@ -200,7 +204,10 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
public <T> Iterable<T> insertAll(Iterable<T> instances) {
Assert.notNull(instances, "Aggregate instances must not be null");
Assert.isTrue(instances.iterator().hasNext(), "Aggregate instances must not be empty");
if (!instances.iterator().hasNext()) {
return Collections.emptyList();
}
List<EntityAndChangeCreator<T>> entityAndChangeCreators = new ArrayList<>();
for (T instance : instances) {
@@ -232,7 +239,10 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
public <T> Iterable<T> updateAll(Iterable<T> instances) {
Assert.notNull(instances, "Aggregate instances must not be null");
Assert.isTrue(instances.iterator().hasNext(), "Aggregate instances must not be empty");
if (!instances.iterator().hasNext()) {
return Collections.emptyList();
}
List<EntityAndChangeCreator<T>> entityAndChangeCreators = new ArrayList<>();
for (T instance : instances) {
@@ -366,7 +376,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);
@@ -395,7 +407,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,31 @@ public class JdbcAggregateTemplateUnitTests {
assertThat(all).containsExactly(alfred2, neumann2);
}
@Test // GH-1401
public void saveAllWithEmptyListDoesNothing() {
assertThat(template.saveAll(emptyList())).isEmpty();
}
@Test // GH-1401
public void insertAllWithEmptyListDoesNothing() {
assertThat(template.insertAll(emptyList())).isEmpty();
}
@Test // GH-1401
public void updateAllWithEmptyListDoesNothing() {
assertThat(template.updateAll(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 {