Skip to content

Commit 1ff403e

Browse files
committed
Allow empty Iterable arguments in JdbcAggregateTemplate again.
This also affects repositories since they delegate to the template. Closes #1401
1 parent 5d3e737 commit 1ff403e

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.jdbc.core;
1717

1818
import java.util.ArrayList;
19+
import java.util.Collections;
1920
import java.util.HashMap;
2021
import java.util.Iterator;
2122
import java.util.LinkedHashMap;
@@ -170,7 +171,11 @@ public <T> T save(T instance) {
170171
@Override
171172
public <T> Iterable<T> saveAll(Iterable<T> instances) {
172173

173-
Assert.isTrue(instances.iterator().hasNext(), "Aggregate instances must not be empty");
174+
Assert.notNull(instances, "Aggregate instances must not be null");
175+
176+
if (!instances.iterator().hasNext()) {
177+
return Collections.emptyList();
178+
}
174179

175180
return performSaveAll(instances);
176181
}
@@ -327,7 +332,9 @@ public <S> void deleteById(Object id, Class<S> domainType) {
327332
@Override
328333
public <T> void deleteAllById(Iterable<?> ids, Class<T> domainType) {
329334

330-
Assert.isTrue(ids.iterator().hasNext(), "Ids must not be empty");
335+
if (!ids.iterator().hasNext()) {
336+
return;
337+
}
331338

332339
BatchingAggregateChange<T, DeleteAggregateChange<T>> batchingAggregateChange = BatchingAggregateChange
333340
.forDelete(domainType);
@@ -356,7 +363,9 @@ public void deleteAll(Class<?> domainType) {
356363
@Override
357364
public <T> void deleteAll(Iterable<? extends T> instances) {
358365

359-
Assert.isTrue(instances.iterator().hasNext(), "Aggregate instances must not be empty");
366+
if (!instances.iterator().hasNext()) {
367+
return;
368+
}
360369

361370
Map<Class, List<Object>> groupedByType = new HashMap<>();
362371

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateUnitTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,21 @@ public void callbackOnLoadPaged() {
312312
assertThat(all).containsExactly(alfred2, neumann2);
313313
}
314314

315+
@Test // GH-1401
316+
public void saveAllWithEmptyListDoesNothing() {
317+
assertThat(template.saveAll(emptyList())).isEmpty();
318+
}
319+
320+
@Test // GH-1401
321+
public void deleteAllWithEmptyListDoesNothing() {
322+
template.deleteAll(emptyList());
323+
}
324+
325+
@Test // GH-1401
326+
public void deleteAllByIdWithEmptyListDoesNothing() {
327+
template.deleteAllById(emptyList(), SampleEntity.class);
328+
}
329+
315330
@Data
316331
@AllArgsConstructor
317332
private static class SampleEntity {

0 commit comments

Comments
 (0)