Skip to content

Commit 1a283fa

Browse files
ctailor2schauder
authored andcommitted
Add DeleteBatchingAggregateChange to batch DeleteRoot actions.
Original pull request #1231 See #537
1 parent 483b30e commit 1a283fa

File tree

12 files changed

+175
-9
lines changed

12 files changed

+175
-9
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ private void execute(DbAction<?> action, JdbcAggregateChangeExecutionContext exe
9898
executionContext.executeDeleteAll((DbAction.DeleteAll<?>) action);
9999
} else if (action instanceof DbAction.DeleteRoot) {
100100
executionContext.executeDeleteRoot((DbAction.DeleteRoot<?>) action);
101+
} else if (action instanceof DbAction.BatchDeleteRoot) {
102+
executionContext.executeBatchDeleteRoot((DbAction.BatchDeleteRoot<?>) action);
101103
} else if (action instanceof DbAction.DeleteAllRoot) {
102104
executionContext.executeDeleteAllRoot((DbAction.DeleteAllRoot<?>) action);
103105
} else if (action instanceof DbAction.AcquireLockRoot) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ <T> void executeDeleteRoot(DbAction.DeleteRoot<T> delete) {
131131
}
132132
}
133133

134+
<T> void executeBatchDeleteRoot(DbAction.BatchDeleteRoot<T> batchDelete) {
135+
136+
List<Object> rootIds = batchDelete.getActions().stream().map(DbAction.DeleteRoot::getId).toList();
137+
accessStrategy.delete(rootIds, batchDelete.getEntityType());
138+
}
139+
134140
<T> void executeDelete(DbAction.Delete<T> delete) {
135141

136142
accessStrategy.delete(delete.getRootId(), delete.getPropertyPath());

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ public void delete(Object id, Class<?> domainType) {
7979
collectVoid(das -> das.delete(id, domainType));
8080
}
8181

82+
@Override
83+
public void delete(Iterable<Object> ids, Class<?> domainType) {
84+
collectVoid(das -> das.delete(ids, domainType));
85+
}
86+
8287
@Override
8388
public <T> void deleteWithVersion(Object id, Class<T> domainType, Number previousVersion) {
8489
collectVoid(das -> das.deleteWithVersion(id, domainType, previousVersion));

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ public interface DataAccessStrategy extends RelationResolver {
130130
*/
131131
void delete(Object id, Class<?> domainType);
132132

133+
/**
134+
* Deletes multiple rows identified by the ids, from the table identified by the domainType. Does not handle cascading
135+
* deletes.
136+
* <P>
137+
* The statement will be of the form : {@code DELETE FROM … WHERE ID IN (:ids) } and throw an optimistic record
138+
* locking exception if no rows have been updated.
139+
*
140+
* @param ids the ids of the rows to be deleted. Must not be {@code null}.
141+
* @param domainType the type of entity to be deleted. Implicitly determines the table to operate on. Must not be
142+
* {@code null}.
143+
* @since 3.0
144+
*/
145+
void delete(Iterable<Object> ids, Class<?> domainType);
146+
133147
/**
134148
* Deletes a single entity from the database and enforce optimistic record locking using the version property. Does
135149
* not handle cascading deletes.
@@ -155,7 +169,8 @@ public interface DataAccessStrategy extends RelationResolver {
155169
/**
156170
* Deletes all entities reachable via {@literal propertyPath} from the instances identified by {@literal rootIds}.
157171
*
158-
* @param rootIds Ids of the root objects on which the {@literal propertyPath} is based. Must not be {@code null} or empty.
172+
* @param rootIds Ids of the root objects on which the {@literal propertyPath} is based. Must not be {@code null} or
173+
* empty.
159174
* @param propertyPath Leading from the root object to the entities to be deleted. Must not be {@code null}.
160175
*/
161176
void delete(Iterable<Object> rootIds, PersistentPropertyPath<RelationalPersistentProperty> propertyPath);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@ public void delete(Object id, Class<?> domainType) {
163163
operations.update(deleteByIdSql, parameter);
164164
}
165165

166+
@Override
167+
public void delete(Iterable<Object> ids, Class<?> domainType) {
168+
169+
String deleteByIdInSql = sql(domainType).getDeleteByIdIn();
170+
SqlParameterSource parameter = sqlParametersFactory.forQueryByIds(ids, domainType);
171+
172+
operations.update(deleteByIdInSql, parameter);
173+
}
174+
166175
@Override
167176
public <T> void deleteWithVersion(Object id, Class<T> domainType, Number previousVersion) {
168177

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ public void delete(Object id, Class<?> domainType) {
8181
delegate.delete(id, domainType);
8282
}
8383

84+
@Override
85+
public void delete(Iterable<Object> ids, Class<?> domainType) {
86+
delegate.delete(ids, domainType);
87+
}
88+
8489
@Override
8590
public <T> void deleteWithVersion(Object id, Class<T> domainType, Number previousVersion) {
8691
delegate.deleteWithVersion(id, domainType, previousVersion);

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

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ class SqlGenerator {
7979
private final Lazy<String> updateSql = Lazy.of(this::createUpdateSql);
8080
private final Lazy<String> updateWithVersionSql = Lazy.of(this::createUpdateWithVersionSql);
8181

82-
private final Lazy<String> deleteByIdSql = Lazy.of(this::createDeleteSql);
82+
private final Lazy<String> deleteByIdSql = Lazy.of(this::createDeleteByIdSql);
83+
private final Lazy<String> deleteByIdInSql = Lazy.of(this::createDeleteByIdInSql);
8384
private final Lazy<String> deleteByIdAndVersionSql = Lazy.of(this::createDeleteByIdAndVersionSql);
85+
private final Lazy<String> deleteByIdInAndVersionSql = Lazy.of(this::createDeleteByIdInAndVersionSql);
8486
private final Lazy<String> deleteByListSql = Lazy.of(this::createDeleteByListSql);
8587

8688
/**
@@ -322,6 +324,15 @@ String getDeleteById() {
322324
return deleteByIdSql.get();
323325
}
324326

327+
/**
328+
* Create a {@code DELETE FROM … WHERE :id IN …} statement.
329+
*
330+
* @return the statement as a {@link String}. Guaranteed to be not {@literal null}.
331+
*/
332+
String getDeleteByIdIn() {
333+
return deleteByIdInSql.get();
334+
}
335+
325336
/**
326337
* Create a {@code DELETE FROM … WHERE :id = … and :___oldOptimisticLockingVersion = ...} statement.
327338
*
@@ -331,6 +342,15 @@ String getDeleteByIdAndVersion() {
331342
return deleteByIdAndVersionSql.get();
332343
}
333344

345+
/**
346+
* Create a {@code DELETE FROM … WHERE :id In … and :___oldOptimisticLockingVersion = ...} statement.
347+
*
348+
* @return the statement as a {@link String}. Guaranteed to be not {@literal null}.
349+
*/
350+
String getDeleteByIdInAndVersion() {
351+
return deleteByIdInAndVersionSql.get();
352+
}
353+
334354
/**
335355
* Create a {@code DELETE FROM … WHERE :ids in (…)} statement.
336356
*
@@ -635,10 +655,14 @@ private UpdateBuilder.UpdateWhereAndOr createBaseUpdate() {
635655
.where(getIdColumn().isEqualTo(getBindMarker(entity.getIdColumn())));
636656
}
637657

638-
private String createDeleteSql() {
658+
private String createDeleteByIdSql() {
639659
return render(createBaseDeleteById(getTable()).build());
640660
}
641661

662+
private String createDeleteByIdInSql() {
663+
return render(createBaseDeleteByIdIn(getTable()).build());
664+
}
665+
642666
private String createDeleteByIdAndVersionSql() {
643667

644668
Delete delete = createBaseDeleteById(getTable()) //
@@ -648,11 +672,25 @@ private String createDeleteByIdAndVersionSql() {
648672
return render(delete);
649673
}
650674

675+
private String createDeleteByIdInAndVersionSql() {
676+
677+
Delete delete = createBaseDeleteByIdIn(getTable()) //
678+
.and(getVersionColumn().isEqualTo(SQL.bindMarker(":" + renderReference(VERSION_SQL_PARAMETER)))) //
679+
.build();
680+
681+
return render(delete);
682+
}
683+
651684
private DeleteBuilder.DeleteWhereAndOr createBaseDeleteById(Table table) {
652685
return Delete.builder().from(table)
653686
.where(getIdColumn().isEqualTo(SQL.bindMarker(":" + renderReference(ID_SQL_PARAMETER))));
654687
}
655688

689+
private DeleteBuilder.DeleteWhereAndOr createBaseDeleteByIdIn(Table table) {
690+
return Delete.builder().from(table)
691+
.where(getIdColumn().in(SQL.bindMarker(":" + renderReference(IDS_SQL_PARAMETER))));
692+
}
693+
656694
private String createDeleteByPathAndCriteria(PersistentPropertyPathExtension path,
657695
Function<Column, Condition> rootCondition) {
658696

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/mybatis/MyBatisDataAccessStrategy.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ public void delete(Object id, Class<?> domainType) {
192192
sqlSession().delete(statement, parameter);
193193
}
194194

195+
@Override
196+
public void delete(Iterable<Object> ids, Class<?> domainType) {
197+
ids.forEach(id -> delete(id, domainType));
198+
}
199+
195200
@Override
196201
public <T> void deleteWithVersion(Object id, Class<T> domainType, Number previousVersion) {
197202

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Collections;
3333
import java.util.HashMap;
3434
import java.util.HashSet;
35+
import java.util.Iterator;
3536
import java.util.List;
3637
import java.util.Map;
3738
import java.util.Set;
@@ -363,6 +364,25 @@ void saveAndDeleteAllByIdsWithReferencedEntity() {
363364
});
364365
}
365366

367+
@Test
368+
void saveAndDeleteAllByAggregateRootsWithVersion() {
369+
AggregateWithImmutableVersion aggregate1 = new AggregateWithImmutableVersion(null, null);
370+
AggregateWithImmutableVersion aggregate2 = new AggregateWithImmutableVersion(null, null);
371+
AggregateWithImmutableVersion aggregate3 = new AggregateWithImmutableVersion(null, null);
372+
Iterator<AggregateWithImmutableVersion> savedAggregatesIterator = template
373+
.saveAll(List.of(aggregate1, aggregate2, aggregate3)).iterator();
374+
AggregateWithImmutableVersion savedAggregate1 = savedAggregatesIterator.next();
375+
AggregateWithImmutableVersion twiceSavedAggregate2 = template.save(savedAggregatesIterator.next());
376+
AggregateWithImmutableVersion twiceSavedAggregate3 = template.save(savedAggregatesIterator.next());
377+
378+
assertThat(template.count(AggregateWithImmutableVersion.class)).isEqualTo(3);
379+
380+
template.deleteAll(List.of(savedAggregate1, twiceSavedAggregate2, twiceSavedAggregate3),
381+
AggregateWithImmutableVersion.class);
382+
383+
assertThat(template.count(AggregateWithImmutableVersion.class)).isEqualTo(0);
384+
}
385+
366386
@Test // DATAJDBC-112
367387
@EnabledOnFeature({ SUPPORTS_QUOTED_IDS, SUPPORTS_GENERATED_IDS_IN_REFERENCED_ENTITIES })
368388
void updateReferencedEntityFromNull() {

spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/DbAction.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,18 @@ public BatchDelete(List<Delete<T>> actions) {
432432
}
433433
}
434434

435+
/**
436+
* Represents a batch delete statement for multiple entities that are aggregate roots.
437+
*
438+
* @param <T> type of the entity for which this represents a database interaction.
439+
* @since 3.0
440+
*/
441+
final class BatchDeleteRoot<T> extends BatchWithValue<T, DeleteRoot<T>, Class<T>> {
442+
public BatchDeleteRoot(List<DeleteRoot<T>> actions) {
443+
super(actions, DeleteRoot::getEntityType);
444+
}
445+
}
446+
435447
/**
436448
* An action depending on another action for providing additional information like the id of a parent entity.
437449
*

0 commit comments

Comments
 (0)