Skip to content

Commit aef1e34

Browse files
committed
Drop superfluous class argument from delete methods in JdbcAggregateTemplate.
Closes: #1315 Original pull request: #1324.
1 parent 01e98dc commit aef1e34

File tree

6 files changed

+60
-24
lines changed

6 files changed

+60
-24
lines changed

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,21 @@ public interface JdbcAggregateOperations {
102102
* {@link org.springframework.dao.OptimisticLockingFailureException}. If no rows match the generated delete operation
103103
* this fact will be silently ignored.
104104
* </p>
105-
*
105+
*
106106
* @param ids the ids of the aggregate roots of the aggregates to be deleted. Must not be {@code null}.
107107
* @param domainType the type of the aggregate root.
108108
* @param <T> the type of the aggregate root.
109109
*/
110110
<T> void deleteAllById(Iterable<?> ids, Class<T> domainType);
111111

112+
/**
113+
* Delete an aggregate identified by its aggregate root.
114+
*
115+
* @param aggregateRoot to delete. Must not be {@code null}.
116+
* @param <T> the type of the aggregate root.
117+
*/
118+
<T> void delete(T aggregateRoot);
119+
112120
/**
113121
* Delete an aggregate identified by its aggregate root.
114122
*
@@ -118,8 +126,12 @@ public interface JdbcAggregateOperations {
118126
* @throws org.springframework.dao.OptimisticLockingFailureException when {@literal T} has a version attribute and the
119127
* version attribute of the provided entity does not match the version attribute in the database, or when
120128
* there is no aggregate root with matching id. In other cases a NOOP delete is silently ignored.
129+
* @deprecated since 3.0 use {@link #delete(Object)} instead
121130
*/
122-
<T> void delete(T aggregateRoot, Class<T> domainType);
131+
@Deprecated
132+
default <T> void delete(T aggregateRoot, Class<T> domainType) {
133+
delete(aggregateRoot);
134+
}
123135

124136
/**
125137
* Delete all aggregates of a given type.
@@ -128,6 +140,14 @@ public interface JdbcAggregateOperations {
128140
*/
129141
void deleteAll(Class<?> domainType);
130142

143+
/**
144+
* Delete all aggregates identified by their aggregate roots.
145+
*
146+
* @param aggregateRoots to delete. Must not be {@code null}.
147+
* @param <T> the type of the aggregate roots.
148+
*/
149+
<T> void deleteAll(Iterable<? extends T> aggregateRoots);
150+
131151
/**
132152
* Delete all aggregates identified by their aggregate roots.
133153
*
@@ -137,8 +157,12 @@ public interface JdbcAggregateOperations {
137157
* @throws org.springframework.dao.OptimisticLockingFailureException when {@literal T} has a version attribute and for at least on entity the
138158
* version attribute of the entity does not match the version attribute in the database, or when
139159
* there is no aggregate root with matching id. In other cases a NOOP delete is silently ignored.
160+
* @deprecated since 3.0 use {@link #deleteAll(Iterable)} instead.
140161
*/
141-
<T> void deleteAll(Iterable<? extends T> aggregateRoots, Class<T> domainType);
162+
@Deprecated
163+
default <T> void deleteAll(Iterable<? extends T> aggregateRoots, Class<T> domainType) {
164+
deleteAll(aggregateRoots);
165+
}
142166

143167
/**
144168
* Counts the number of aggregates of a given type.

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

Lines changed: 20 additions & 3 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.HashMap;
1920
import java.util.Iterator;
2021
import java.util.LinkedHashMap;
2122
import java.util.List;
@@ -303,11 +304,11 @@ public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
303304
}
304305

305306
@Override
306-
public <S> void delete(S aggregateRoot, Class<S> domainType) {
307+
public <S> void delete(S aggregateRoot) {
307308

308309
Assert.notNull(aggregateRoot, "Aggregate root must not be null");
309-
Assert.notNull(domainType, "Domain type must not be null");
310310

311+
Class<S> domainType = (Class<S>) aggregateRoot.getClass();
311312
IdentifierAccessor identifierAccessor = context.getRequiredPersistentEntity(domainType)
312313
.getIdentifierAccessor(aggregateRoot);
313314

@@ -353,10 +354,26 @@ public void deleteAll(Class<?> domainType) {
353354
}
354355

355356
@Override
356-
public <T> void deleteAll(Iterable<? extends T> instances, Class<T> domainType) {
357+
public <T> void deleteAll(Iterable<? extends T> instances) {
357358

358359
Assert.isTrue(instances.iterator().hasNext(), "Aggregate instances must not be empty");
359360

361+
Map<Class, List<Object>> groupedByType = new HashMap<>();
362+
363+
for (T instance : instances) {
364+
Class<?> type = instance.getClass();
365+
final List<Object> list = groupedByType.computeIfAbsent(type, __ -> new ArrayList<>());
366+
list.add(instance);
367+
}
368+
369+
for (Class type : groupedByType.keySet()) {
370+
doDeleteAll(groupedByType.get(type), type);
371+
}
372+
}
373+
374+
private <T> void doDeleteAll(Iterable<? extends T> instances, Class<T> domainType) {
375+
376+
360377
BatchingAggregateChange<T, DeleteAggregateChange<T>> batchingAggregateChange = BatchingAggregateChange
361378
.forDelete(domainType);
362379
Map<Object, T> instancesBeforeExecute = new LinkedHashMap<>();

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/SimpleJdbcRepository.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,11 @@
2525
import org.springframework.data.jdbc.core.JdbcAggregateOperations;
2626
import org.springframework.data.jdbc.core.convert.JdbcConverter;
2727
import org.springframework.data.mapping.PersistentEntity;
28-
import org.springframework.data.relational.core.query.Query;
2928
import org.springframework.data.relational.repository.query.RelationalExampleMapper;
3029
import org.springframework.data.repository.CrudRepository;
3130
import org.springframework.data.repository.PagingAndSortingRepository;
3231
import org.springframework.data.repository.query.FluentQuery;
3332
import org.springframework.data.repository.query.QueryByExampleExecutor;
34-
import org.springframework.data.support.PageableExecutionUtils;
35-
import org.springframework.data.util.Streamable;
3633
import org.springframework.transaction.annotation.Transactional;
3734
import org.springframework.util.Assert;
3835

@@ -110,7 +107,7 @@ public void deleteById(ID id) {
110107
@Transactional
111108
@Override
112109
public void delete(T instance) {
113-
entityOperations.delete(instance, entity.getType());
110+
entityOperations.delete(instance);
114111
}
115112

116113
@Override
@@ -121,7 +118,7 @@ public void deleteAllById(Iterable<? extends ID> ids) {
121118
@Transactional
122119
@Override
123120
public void deleteAll(Iterable<? extends T> entities) {
124-
entityOperations.deleteAll(entities, entity.getType());
121+
entityOperations.deleteAll(entities);
125122
}
126123

127124
@Transactional

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public void saveAndDeleteAnEntityWithReferencedEntity() {
161161

162162
LegoSet saved = template.save(legoSet);
163163

164-
template.delete(saved, LegoSet.class);
164+
template.delete(saved);
165165

166166
SoftAssertions softly = new SoftAssertions();
167167

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ void saveAndDeleteAnEntityWithReferencedEntity() {
305305

306306
template.save(legoSet);
307307

308-
template.delete(legoSet, LegoSet.class);
308+
template.delete(legoSet);
309309

310310
assertSoftly(softly -> {
311311

@@ -338,7 +338,7 @@ void saveAndDeleteAllByAggregateRootsWithReferencedEntity() {
338338
LegoSet legoSet2 = template.save(createLegoSet("Some Name"));
339339
template.save(createLegoSet("Some other Name"));
340340

341-
template.deleteAll(List.of(legoSet1, legoSet2), LegoSet.class);
341+
template.deleteAll(List.of(legoSet1, legoSet2));
342342

343343
assertSoftly(softly -> {
344344

@@ -378,8 +378,7 @@ void saveAndDeleteAllByAggregateRootsWithVersion() {
378378

379379
assertThat(template.count(AggregateWithImmutableVersion.class)).isEqualTo(3);
380380

381-
template.deleteAll(List.of(savedAggregate1, twiceSavedAggregate2, twiceSavedAggregate3),
382-
AggregateWithImmutableVersion.class);
381+
template.deleteAll(List.of(savedAggregate1, twiceSavedAggregate2, twiceSavedAggregate3));
383382

384383
assertThat(template.count(AggregateWithImmutableVersion.class)).isEqualTo(0);
385384
}
@@ -737,7 +736,7 @@ void saveAndLoadLongChain() {
737736
assertThat(reloaded.four).isEqualTo(chain4.four);
738737
assertThat(reloaded.chain3.chain2.chain1.chain0.zeroValue).isEqualTo(chain4.chain3.chain2.chain1.chain0.zeroValue);
739738

740-
template.delete(chain4, Chain4.class);
739+
template.delete(chain4);
741740

742741
assertThat(count("CHAIN0")).isEqualTo(0);
743742
}
@@ -768,7 +767,7 @@ void saveAndLoadLongChainWithoutIds() {
768767
assertThat(reloaded.four).isEqualTo(chain4.four);
769768
assertThat(reloaded.chain3.chain2.chain1.chain0.zeroValue).isEqualTo(chain4.chain3.chain2.chain1.chain0.zeroValue);
770769

771-
template.delete(chain4, NoIdChain4.class);
770+
template.delete(chain4);
772771

773772
assertThat(count("CHAIN0")).isEqualTo(0);
774773
}
@@ -913,17 +912,17 @@ void deleteAggregateWithVersion() {
913912
final Long id = aggregate.getId();
914913

915914
assertThatThrownBy(
916-
() -> template.delete(new AggregateWithImmutableVersion(id, 0L), AggregateWithImmutableVersion.class))
915+
() -> template.delete(new AggregateWithImmutableVersion(id, 0L)))
917916
.describedAs("deleting an aggregate with an outdated version should raise an exception")
918917
.isInstanceOf(OptimisticLockingFailureException.class);
919918

920919
assertThatThrownBy(
921-
() -> template.delete(new AggregateWithImmutableVersion(id, 2L), AggregateWithImmutableVersion.class))
920+
() -> template.delete(new AggregateWithImmutableVersion(id, 2L)))
922921
.describedAs("deleting an aggregate with a future version should raise an exception")
923922
.isInstanceOf(OptimisticLockingFailureException.class);
924923

925924
// This should succeed
926-
template.delete(aggregate, AggregateWithImmutableVersion.class);
925+
template.delete(aggregate);
927926

928927
aggregate = new AggregateWithImmutableVersion(null, null);
929928
aggregate = template.save(aggregate);

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.mockito.ArgumentCaptor;
3131
import org.mockito.Mock;
3232
import org.mockito.junit.jupiter.MockitoExtension;
33-
3433
import org.springframework.context.ApplicationEventPublisher;
3534
import org.springframework.data.annotation.Id;
3635
import org.springframework.data.annotation.Version;
@@ -247,7 +246,7 @@ void deletePreparesChangeWithPreviousVersion_onDeleteByInstance() {
247246
EntityWithImmutableVersion entity = new EntityWithImmutableVersion(1L, 1L);
248247
when(callbacks.callback(any(), any(), any())).thenReturn(entity, entity);
249248

250-
template.delete(entity, EntityWithImmutableVersion.class);
249+
template.delete(entity);
251250

252251
ArgumentCaptor<Object> aggregateChangeCaptor = ArgumentCaptor.forClass(Object.class);
253252
verify(callbacks).callback(eq(BeforeDeleteCallback.class), any(), aggregateChangeCaptor.capture());
@@ -264,7 +263,7 @@ public void callbackOnDelete() {
264263

265264
when(callbacks.callback(any(Class.class), any(), any())).thenReturn(second);
266265

267-
template.delete(first, SampleEntity.class);
266+
template.delete(first);
268267

269268
verify(callbacks).callback(eq(BeforeDeleteCallback.class), eq(first), any(MutableAggregateChange.class));
270269
verify(callbacks).callback(AfterDeleteCallback.class, second);

0 commit comments

Comments
 (0)