Skip to content

Commit a7cea62

Browse files
committed
Remove code duplication.
Mainly reimplements the changes undone in 49e343f. The check for presence of the ID property is implemented for all variants for save, as it should. See #1924
1 parent 8e710c9 commit a7cea62

File tree

1 file changed

+8
-32
lines changed

1 file changed

+8
-32
lines changed

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

+8-32
Original file line numberDiff line numberDiff line change
@@ -173,19 +173,7 @@ public <T> T save(T instance) {
173173

174174
@Override
175175
public <T> List<T> saveAll(Iterable<T> instances) {
176-
177-
Assert.notNull(instances, "Aggregate instances must not be null");
178-
179-
if (!instances.iterator().hasNext()) {
180-
return Collections.emptyList();
181-
}
182-
183-
List<EntityAndChangeCreator<T>> entityAndChangeCreators = new ArrayList<>();
184-
for (T instance : instances) {
185-
verifyIdProperty(instance);
186-
entityAndChangeCreators.add(new EntityAndChangeCreator<>(instance, changeCreatorSelectorForSave(instance)));
187-
}
188-
return performSaveAll(entityAndChangeCreators);
176+
return doInBatch(instances, (first) -> (second -> changeCreatorSelectorForSave(first).apply(second)));
189177
}
190178

191179
/**
@@ -206,21 +194,7 @@ public <T> T insert(T instance) {
206194

207195
@Override
208196
public <T> List<T> insertAll(Iterable<T> instances) {
209-
210-
Assert.notNull(instances, "Aggregate instances must not be null");
211-
212-
if (!instances.iterator().hasNext()) {
213-
return Collections.emptyList();
214-
}
215-
216-
List<EntityAndChangeCreator<T>> entityAndChangeCreators = new ArrayList<>();
217-
for (T instance : instances) {
218-
219-
Function<T, RootAggregateChange<T>> changeCreator = entity -> createInsertChange(prepareVersionForInsert(entity));
220-
EntityAndChangeCreator<T> entityChange = new EntityAndChangeCreator<>(instance, changeCreator);
221-
entityAndChangeCreators.add(entityChange);
222-
}
223-
return performSaveAll(entityAndChangeCreators);
197+
return doInBatch(instances, (__) -> (entity -> createInsertChange(prepareVersionForInsert(entity))));
224198
}
225199

226200
/**
@@ -241,6 +215,10 @@ public <T> T update(T instance) {
241215

242216
@Override
243217
public <T> List<T> updateAll(Iterable<T> instances) {
218+
return doInBatch(instances, (__) -> (entity -> createUpdateChange(prepareVersionForUpdate(entity))));
219+
}
220+
221+
private <T> List<T> doInBatch(Iterable<T> instances,Function<T, Function<T, RootAggregateChange<T>>> changeCreatorFunction) {
244222

245223
Assert.notNull(instances, "Aggregate instances must not be null");
246224

@@ -250,10 +228,8 @@ public <T> List<T> updateAll(Iterable<T> instances) {
250228

251229
List<EntityAndChangeCreator<T>> entityAndChangeCreators = new ArrayList<>();
252230
for (T instance : instances) {
253-
254-
Function<T, RootAggregateChange<T>> changeCreator = entity -> createUpdateChange(prepareVersionForUpdate(entity));
255-
EntityAndChangeCreator<T> entityChange = new EntityAndChangeCreator<>(instance, changeCreator);
256-
entityAndChangeCreators.add(entityChange);
231+
verifyIdProperty(instance);
232+
entityAndChangeCreators.add(new EntityAndChangeCreator<T>(instance, changeCreatorFunction.apply(instance)));
257233
}
258234
return performSaveAll(entityAndChangeCreators);
259235
}

0 commit comments

Comments
 (0)