From a7cea62ed7928e930760120af05e74263928fad1 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Tue, 29 Oct 2024 13:08:46 +0100 Subject: [PATCH] Remove code duplication. Mainly reimplements the changes undone in 49e343fe8ac1d0372648cd97b0d5783d852272eb. The check for presence of the ID property is implemented for all variants for save, as it should. See #1924 --- .../data/jdbc/core/JdbcAggregateTemplate.java | 40 ++++--------------- 1 file changed, 8 insertions(+), 32 deletions(-) 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); }