Skip to content

Commit e325c01

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

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

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

Lines changed: 19 additions & 5 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;
@@ -171,7 +172,10 @@ public <T> T save(T instance) {
171172
public <T> Iterable<T> saveAll(Iterable<T> instances) {
172173

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

176180
List<EntityAndChangeCreator<T>> entityAndChangeCreators = new ArrayList<>();
177181
for (T instance : instances) {
@@ -200,7 +204,10 @@ public <T> T insert(T instance) {
200204
public <T> Iterable<T> insertAll(Iterable<T> instances) {
201205

202206
Assert.notNull(instances, "Aggregate instances must not be null");
203-
Assert.isTrue(instances.iterator().hasNext(), "Aggregate instances must not be empty");
207+
208+
if (!instances.iterator().hasNext()) {
209+
return Collections.emptyList();
210+
}
204211

205212
List<EntityAndChangeCreator<T>> entityAndChangeCreators = new ArrayList<>();
206213
for (T instance : instances) {
@@ -232,7 +239,10 @@ public <T> T update(T instance) {
232239
public <T> Iterable<T> updateAll(Iterable<T> instances) {
233240

234241
Assert.notNull(instances, "Aggregate instances must not be null");
235-
Assert.isTrue(instances.iterator().hasNext(), "Aggregate instances must not be empty");
242+
243+
if (!instances.iterator().hasNext()) {
244+
return Collections.emptyList();
245+
}
236246

237247
List<EntityAndChangeCreator<T>> entityAndChangeCreators = new ArrayList<>();
238248
for (T instance : instances) {
@@ -366,7 +376,9 @@ public <S> void deleteById(Object id, Class<S> domainType) {
366376
@Override
367377
public <T> void deleteAllById(Iterable<?> ids, Class<T> domainType) {
368378

369-
Assert.isTrue(ids.iterator().hasNext(), "Ids must not be empty");
379+
if (!ids.iterator().hasNext()) {
380+
return;
381+
}
370382

371383
BatchingAggregateChange<T, DeleteAggregateChange<T>> batchingAggregateChange = BatchingAggregateChange
372384
.forDelete(domainType);
@@ -395,7 +407,9 @@ public void deleteAll(Class<?> domainType) {
395407
@Override
396408
public <T> void deleteAll(Iterable<? extends T> instances) {
397409

398-
Assert.isTrue(instances.iterator().hasNext(), "Aggregate instances must not be empty");
410+
if (!instances.iterator().hasNext()) {
411+
return;
412+
}
399413

400414
Map<Class, List<Object>> groupedByType = new HashMap<>();
401415

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,31 @@ 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 insertAllWithEmptyListDoesNothing() {
322+
assertThat(template.insertAll(emptyList())).isEmpty();
323+
}
324+
325+
@Test // GH-1401
326+
public void updateAllWithEmptyListDoesNothing() {
327+
assertThat(template.updateAll(emptyList())).isEmpty();
328+
}
329+
330+
@Test // GH-1401
331+
public void deleteAllWithEmptyListDoesNothing() {
332+
template.deleteAll(emptyList());
333+
}
334+
335+
@Test // GH-1401
336+
public void deleteAllByIdWithEmptyListDoesNothing() {
337+
template.deleteAllById(emptyList(), SampleEntity.class);
338+
}
339+
315340
@Data
316341
@AllArgsConstructor
317342
private static class SampleEntity {

0 commit comments

Comments
 (0)