diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java index dbef6d1e1a..520211d439 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java @@ -173,19 +173,7 @@ public T save(T instance) { @Override public List saveAll(Iterable instances) { - - Assert.notNull(instances, "Aggregate instances must not be null"); - - if (!instances.iterator().hasNext()) { - return Collections.emptyList(); - } - - List> entityAndChangeCreators = new ArrayList<>(); - for (T instance : instances) { - verifyIdProperty(instance); - entityAndChangeCreators.add(new EntityAndChangeCreator<>(instance, changeCreatorSelectorForSave(instance))); - } - return performSaveAll(entityAndChangeCreators); + return doInBatch(instances, (first) -> (second -> changeCreatorSelectorForSave(first).apply(second))); } /** @@ -206,21 +194,7 @@ public T insert(T instance) { @Override public List insertAll(Iterable instances) { - - Assert.notNull(instances, "Aggregate instances must not be null"); - - if (!instances.iterator().hasNext()) { - return Collections.emptyList(); - } - - List> entityAndChangeCreators = new ArrayList<>(); - for (T instance : instances) { - - Function> changeCreator = entity -> createInsertChange(prepareVersionForInsert(entity)); - EntityAndChangeCreator entityChange = new EntityAndChangeCreator<>(instance, changeCreator); - entityAndChangeCreators.add(entityChange); - } - return performSaveAll(entityAndChangeCreators); + return doInBatch(instances, (__) -> (entity -> createInsertChange(prepareVersionForInsert(entity)))); } /** @@ -241,6 +215,10 @@ public T update(T instance) { @Override public List updateAll(Iterable instances) { + return doInBatch(instances, (__) -> (entity -> createUpdateChange(prepareVersionForUpdate(entity)))); + } + + private List doInBatch(Iterable instances,Function>> changeCreatorFunction) { Assert.notNull(instances, "Aggregate instances must not be null"); @@ -250,10 +228,8 @@ public List updateAll(Iterable instances) { List> entityAndChangeCreators = new ArrayList<>(); for (T instance : instances) { - - Function> changeCreator = entity -> createUpdateChange(prepareVersionForUpdate(entity)); - EntityAndChangeCreator entityChange = new EntityAndChangeCreator<>(instance, changeCreator); - entityAndChangeCreators.add(entityChange); + verifyIdProperty(instance); + entityAndChangeCreators.add(new EntityAndChangeCreator(instance, changeCreatorFunction.apply(instance))); } return performSaveAll(entityAndChangeCreators); }