Browse Source

Drop superfluous class argument from delete methods in `JdbcAggregateTemplate`.

Closes: #1315
Original pull request: #1324.
pull/1348/head
Jens Schauder 3 years ago committed by Jens Schauder
parent
commit
aef1e34f5e
  1. 30
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateOperations.java
  2. 23
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java
  3. 7
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/SimpleJdbcRepository.java
  4. 2
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/ImmutableAggregateTemplateHsqlIntegrationTests.java
  5. 17
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java
  6. 5
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateUnitTests.java

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

@ -102,13 +102,21 @@ public interface JdbcAggregateOperations { @@ -102,13 +102,21 @@ public interface JdbcAggregateOperations {
* {@link org.springframework.dao.OptimisticLockingFailureException}. If no rows match the generated delete operation
* this fact will be silently ignored.
* </p>
*
*
* @param ids the ids of the aggregate roots of the aggregates to be deleted. Must not be {@code null}.
* @param domainType the type of the aggregate root.
* @param <T> the type of the aggregate root.
*/
<T> void deleteAllById(Iterable<?> ids, Class<T> domainType);
/**
* Delete an aggregate identified by its aggregate root.
*
* @param aggregateRoot to delete. Must not be {@code null}.
* @param <T> the type of the aggregate root.
*/
<T> void delete(T aggregateRoot);
/**
* Delete an aggregate identified by its aggregate root.
*
@ -118,8 +126,12 @@ public interface JdbcAggregateOperations { @@ -118,8 +126,12 @@ public interface JdbcAggregateOperations {
* @throws org.springframework.dao.OptimisticLockingFailureException when {@literal T} has a version attribute and the
* version attribute of the provided entity does not match the version attribute in the database, or when
* there is no aggregate root with matching id. In other cases a NOOP delete is silently ignored.
* @deprecated since 3.0 use {@link #delete(Object)} instead
*/
<T> void delete(T aggregateRoot, Class<T> domainType);
@Deprecated
default <T> void delete(T aggregateRoot, Class<T> domainType) {
delete(aggregateRoot);
}
/**
* Delete all aggregates of a given type.
@ -128,6 +140,14 @@ public interface JdbcAggregateOperations { @@ -128,6 +140,14 @@ public interface JdbcAggregateOperations {
*/
void deleteAll(Class<?> domainType);
/**
* Delete all aggregates identified by their aggregate roots.
*
* @param aggregateRoots to delete. Must not be {@code null}.
* @param <T> the type of the aggregate roots.
*/
<T> void deleteAll(Iterable<? extends T> aggregateRoots);
/**
* Delete all aggregates identified by their aggregate roots.
*
@ -137,8 +157,12 @@ public interface JdbcAggregateOperations { @@ -137,8 +157,12 @@ public interface JdbcAggregateOperations {
* @throws org.springframework.dao.OptimisticLockingFailureException when {@literal T} has a version attribute and for at least on entity the
* version attribute of the entity does not match the version attribute in the database, or when
* there is no aggregate root with matching id. In other cases a NOOP delete is silently ignored.
* @deprecated since 3.0 use {@link #deleteAll(Iterable)} instead.
*/
<T> void deleteAll(Iterable<? extends T> aggregateRoots, Class<T> domainType);
@Deprecated
default <T> void deleteAll(Iterable<? extends T> aggregateRoots, Class<T> domainType) {
deleteAll(aggregateRoots);
}
/**
* Counts the number of aggregates of a given type.

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

@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.data.jdbc.core;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
@ -303,11 +304,11 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations { @@ -303,11 +304,11 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
}
@Override
public <S> void delete(S aggregateRoot, Class<S> domainType) {
public <S> void delete(S aggregateRoot) {
Assert.notNull(aggregateRoot, "Aggregate root must not be null");
Assert.notNull(domainType, "Domain type must not be null");
Class<S> domainType = (Class<S>) aggregateRoot.getClass();
IdentifierAccessor identifierAccessor = context.getRequiredPersistentEntity(domainType)
.getIdentifierAccessor(aggregateRoot);
@ -353,10 +354,26 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations { @@ -353,10 +354,26 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
}
@Override
public <T> void deleteAll(Iterable<? extends T> instances, Class<T> domainType) {
public <T> void deleteAll(Iterable<? extends T> instances) {
Assert.isTrue(instances.iterator().hasNext(), "Aggregate instances must not be empty");
Map<Class, List<Object>> groupedByType = new HashMap<>();
for (T instance : instances) {
Class<?> type = instance.getClass();
final List<Object> list = groupedByType.computeIfAbsent(type, __ -> new ArrayList<>());
list.add(instance);
}
for (Class type : groupedByType.keySet()) {
doDeleteAll(groupedByType.get(type), type);
}
}
private <T> void doDeleteAll(Iterable<? extends T> instances, Class<T> domainType) {
BatchingAggregateChange<T, DeleteAggregateChange<T>> batchingAggregateChange = BatchingAggregateChange
.forDelete(domainType);
Map<Object, T> instancesBeforeExecute = new LinkedHashMap<>();

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

@ -25,14 +25,11 @@ import org.springframework.data.domain.Sort; @@ -25,14 +25,11 @@ import org.springframework.data.domain.Sort;
import org.springframework.data.jdbc.core.JdbcAggregateOperations;
import org.springframework.data.jdbc.core.convert.JdbcConverter;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.relational.core.query.Query;
import org.springframework.data.relational.repository.query.RelationalExampleMapper;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.FluentQuery;
import org.springframework.data.repository.query.QueryByExampleExecutor;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.data.util.Streamable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
@ -110,7 +107,7 @@ public class SimpleJdbcRepository<T, ID> @@ -110,7 +107,7 @@ public class SimpleJdbcRepository<T, ID>
@Transactional
@Override
public void delete(T instance) {
entityOperations.delete(instance, entity.getType());
entityOperations.delete(instance);
}
@Override
@ -121,7 +118,7 @@ public class SimpleJdbcRepository<T, ID> @@ -121,7 +118,7 @@ public class SimpleJdbcRepository<T, ID>
@Transactional
@Override
public void deleteAll(Iterable<? extends T> entities) {
entityOperations.deleteAll(entities, entity.getType());
entityOperations.deleteAll(entities);
}
@Transactional

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

@ -161,7 +161,7 @@ public class ImmutableAggregateTemplateHsqlIntegrationTests { @@ -161,7 +161,7 @@ public class ImmutableAggregateTemplateHsqlIntegrationTests {
LegoSet saved = template.save(legoSet);
template.delete(saved, LegoSet.class);
template.delete(saved);
SoftAssertions softly = new SoftAssertions();

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

@ -305,7 +305,7 @@ class JdbcAggregateTemplateIntegrationTests { @@ -305,7 +305,7 @@ class JdbcAggregateTemplateIntegrationTests {
template.save(legoSet);
template.delete(legoSet, LegoSet.class);
template.delete(legoSet);
assertSoftly(softly -> {
@ -338,7 +338,7 @@ class JdbcAggregateTemplateIntegrationTests { @@ -338,7 +338,7 @@ class JdbcAggregateTemplateIntegrationTests {
LegoSet legoSet2 = template.save(createLegoSet("Some Name"));
template.save(createLegoSet("Some other Name"));
template.deleteAll(List.of(legoSet1, legoSet2), LegoSet.class);
template.deleteAll(List.of(legoSet1, legoSet2));
assertSoftly(softly -> {
@ -378,8 +378,7 @@ class JdbcAggregateTemplateIntegrationTests { @@ -378,8 +378,7 @@ class JdbcAggregateTemplateIntegrationTests {
assertThat(template.count(AggregateWithImmutableVersion.class)).isEqualTo(3);
template.deleteAll(List.of(savedAggregate1, twiceSavedAggregate2, twiceSavedAggregate3),
AggregateWithImmutableVersion.class);
template.deleteAll(List.of(savedAggregate1, twiceSavedAggregate2, twiceSavedAggregate3));
assertThat(template.count(AggregateWithImmutableVersion.class)).isEqualTo(0);
}
@ -737,7 +736,7 @@ class JdbcAggregateTemplateIntegrationTests { @@ -737,7 +736,7 @@ class JdbcAggregateTemplateIntegrationTests {
assertThat(reloaded.four).isEqualTo(chain4.four);
assertThat(reloaded.chain3.chain2.chain1.chain0.zeroValue).isEqualTo(chain4.chain3.chain2.chain1.chain0.zeroValue);
template.delete(chain4, Chain4.class);
template.delete(chain4);
assertThat(count("CHAIN0")).isEqualTo(0);
}
@ -768,7 +767,7 @@ class JdbcAggregateTemplateIntegrationTests { @@ -768,7 +767,7 @@ class JdbcAggregateTemplateIntegrationTests {
assertThat(reloaded.four).isEqualTo(chain4.four);
assertThat(reloaded.chain3.chain2.chain1.chain0.zeroValue).isEqualTo(chain4.chain3.chain2.chain1.chain0.zeroValue);
template.delete(chain4, NoIdChain4.class);
template.delete(chain4);
assertThat(count("CHAIN0")).isEqualTo(0);
}
@ -913,17 +912,17 @@ class JdbcAggregateTemplateIntegrationTests { @@ -913,17 +912,17 @@ class JdbcAggregateTemplateIntegrationTests {
final Long id = aggregate.getId();
assertThatThrownBy(
() -> template.delete(new AggregateWithImmutableVersion(id, 0L), AggregateWithImmutableVersion.class))
() -> template.delete(new AggregateWithImmutableVersion(id, 0L)))
.describedAs("deleting an aggregate with an outdated version should raise an exception")
.isInstanceOf(OptimisticLockingFailureException.class);
assertThatThrownBy(
() -> template.delete(new AggregateWithImmutableVersion(id, 2L), AggregateWithImmutableVersion.class))
() -> template.delete(new AggregateWithImmutableVersion(id, 2L)))
.describedAs("deleting an aggregate with a future version should raise an exception")
.isInstanceOf(OptimisticLockingFailureException.class);
// This should succeed
template.delete(aggregate, AggregateWithImmutableVersion.class);
template.delete(aggregate);
aggregate = new AggregateWithImmutableVersion(null, null);
aggregate = template.save(aggregate);

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

@ -30,7 +30,6 @@ import org.junit.jupiter.api.extension.ExtendWith; @@ -30,7 +30,6 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
@ -247,7 +246,7 @@ public class JdbcAggregateTemplateUnitTests { @@ -247,7 +246,7 @@ public class JdbcAggregateTemplateUnitTests {
EntityWithImmutableVersion entity = new EntityWithImmutableVersion(1L, 1L);
when(callbacks.callback(any(), any(), any())).thenReturn(entity, entity);
template.delete(entity, EntityWithImmutableVersion.class);
template.delete(entity);
ArgumentCaptor<Object> aggregateChangeCaptor = ArgumentCaptor.forClass(Object.class);
verify(callbacks).callback(eq(BeforeDeleteCallback.class), any(), aggregateChangeCaptor.capture());
@ -264,7 +263,7 @@ public class JdbcAggregateTemplateUnitTests { @@ -264,7 +263,7 @@ public class JdbcAggregateTemplateUnitTests {
when(callbacks.callback(any(Class.class), any(), any())).thenReturn(second);
template.delete(first, SampleEntity.class);
template.delete(first);
verify(callbacks).callback(eq(BeforeDeleteCallback.class), eq(first), any(MutableAggregateChange.class));
verify(callbacks).callback(AfterDeleteCallback.class, second);

Loading…
Cancel
Save