Introducing AfterConvertCallback/Event.

This is to replace the AfterLoadCallback/Event in order to match the naming of other store modules.

AfterLoadCallback/Event is still in place for now, but deprecated.

Closes #1053
Original pull request: #1060.
This commit is contained in:
Jens Schauder
2021-09-24 09:13:11 +02:00
committed by Mark Paluch
parent b02ee3c083
commit 3cd25ee01f
12 changed files with 154 additions and 25 deletions

View File

@@ -208,7 +208,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
T entity = accessStrategy.findById(id, domainType);
if (entity != null) {
return triggerAfterLoad(entity);
return triggerAfterConvert(entity);
}
return entity;
}
@@ -236,7 +236,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
Assert.notNull(domainType, "Domain type must not be null!");
Iterable<T> all = accessStrategy.findAll(domainType, sort);
return triggerAfterLoad(all);
return triggerAfterConvert(all);
}
/*
@@ -248,7 +248,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
Assert.notNull(domainType, "Domain type must not be null!");
Iterable<T> items = triggerAfterLoad(accessStrategy.findAll(domainType, pageable));
Iterable<T> items = triggerAfterConvert(accessStrategy.findAll(domainType, pageable));
List<T> content = StreamSupport.stream(items.spliterator(), false).collect(Collectors.toList());
return PageableExecutionUtils.getPage(content, pageable, () -> accessStrategy.count(domainType));
@@ -264,7 +264,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
Assert.notNull(domainType, "Domain type must not be null!");
Iterable<T> all = accessStrategy.findAll(domainType);
return triggerAfterLoad(all);
return triggerAfterConvert(all);
}
/*
@@ -278,7 +278,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
Assert.notNull(domainType, "Domain type must not be null!");
Iterable<T> allById = accessStrategy.findAllById(ids, domainType);
return triggerAfterLoad(allById);
return triggerAfterConvert(allById);
}
/*
@@ -385,22 +385,24 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
return aggregateChange;
}
private <T> Iterable<T> triggerAfterLoad(Iterable<T> all) {
private <T> Iterable<T> triggerAfterConvert(Iterable<T> all) {
List<T> result = new ArrayList<>();
for (T e : all) {
result.add(triggerAfterLoad(e));
result.add(triggerAfterConvert(e));
}
return result;
}
private <T> T triggerAfterLoad(T entity) {
private <T> T triggerAfterConvert(T entity) {
publisher.publishEvent(new AfterLoadEvent<>(entity));
publisher.publishEvent(new AfterConvertEvent<>(entity));
return entityCallbacks.callback(AfterLoadCallback.class, entity);
entity = entityCallbacks.callback(AfterLoadCallback.class, entity);
return entityCallbacks.callback(AfterConvertCallback.class, entity);
}
private <T> T triggerBeforeConvert(T aggregateRoot) {

View File

@@ -32,6 +32,8 @@ import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.relational.core.dialect.Dialect;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.event.AfterConvertCallback;
import org.springframework.data.relational.core.mapping.event.AfterConvertEvent;
import org.springframework.data.relational.core.mapping.event.AfterLoadCallback;
import org.springframework.data.relational.core.mapping.event.AfterLoadEvent;
import org.springframework.data.repository.core.NamedQueries;
@@ -157,9 +159,11 @@ class JdbcQueryLookupStrategy implements QueryLookupStrategy {
if (entity != null) {
publisher.publishEvent(new AfterLoadEvent<>(entity));
publisher.publishEvent(new AfterConvertEvent<>(entity));
if (callbacks != null) {
return callbacks.callback(AfterLoadCallback.class, entity);
entity = callbacks.callback(AfterLoadCallback.class, entity);
return callbacks.callback(AfterConvertCallback.class, entity);
}
}

View File

@@ -41,6 +41,7 @@ import org.springframework.data.relational.core.conversion.MutableAggregateChang
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.NamingStrategy;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.event.AfterConvertCallback;
import org.springframework.data.relational.core.mapping.event.AfterDeleteCallback;
import org.springframework.data.relational.core.mapping.event.AfterLoadCallback;
import org.springframework.data.relational.core.mapping.event.AfterSaveCallback;
@@ -136,7 +137,9 @@ public class JdbcAggregateTemplateUnitTests {
when(dataAccessStrategy.findAll(SampleEntity.class)).thenReturn(asList(alfred1, neumann1));
when(callbacks.callback(any(Class.class), eq(alfred1), any())).thenReturn(alfred2);
when(callbacks.callback(any(Class.class), eq(alfred2), any())).thenReturn(alfred2);
when(callbacks.callback(any(Class.class), eq(neumann1), any())).thenReturn(neumann2);
when(callbacks.callback(any(Class.class), eq(neumann2), any())).thenReturn(neumann2);
Iterable<SampleEntity> all = template.findAll(SampleEntity.class);
@@ -158,12 +161,16 @@ public class JdbcAggregateTemplateUnitTests {
when(dataAccessStrategy.findAll(SampleEntity.class, Sort.by("name"))).thenReturn(asList(alfred1, neumann1));
when(callbacks.callback(any(Class.class), eq(alfred1), any())).thenReturn(alfred2);
when(callbacks.callback(any(Class.class), eq(alfred2), any())).thenReturn(alfred2);
when(callbacks.callback(any(Class.class), eq(neumann1), any())).thenReturn(neumann2);
when(callbacks.callback(any(Class.class), eq(neumann2), any())).thenReturn(neumann2);
Iterable<SampleEntity> all = template.findAll(SampleEntity.class, Sort.by("name"));
verify(callbacks).callback(AfterLoadCallback.class, alfred1);
verify(callbacks).callback(AfterConvertCallback.class, alfred2);
verify(callbacks).callback(AfterLoadCallback.class, neumann1);
verify(callbacks).callback(AfterConvertCallback.class, neumann2);
assertThat(all).containsExactly(alfred2, neumann2);
}
@@ -180,12 +187,16 @@ public class JdbcAggregateTemplateUnitTests {
when(dataAccessStrategy.findAll(SampleEntity.class, PageRequest.of(0, 20))).thenReturn(asList(alfred1, neumann1));
when(callbacks.callback(any(Class.class), eq(alfred1), any())).thenReturn(alfred2);
when(callbacks.callback(any(Class.class), eq(alfred2), any())).thenReturn(alfred2);
when(callbacks.callback(any(Class.class), eq(neumann1), any())).thenReturn(neumann2);
when(callbacks.callback(any(Class.class), eq(neumann2), any())).thenReturn(neumann2);
Iterable<SampleEntity> all = template.findAll(SampleEntity.class, PageRequest.of(0, 20));
verify(callbacks).callback(AfterLoadCallback.class, alfred1);
verify(callbacks).callback(AfterConvertCallback.class, alfred2);
verify(callbacks).callback(AfterLoadCallback.class, neumann1);
verify(callbacks).callback(AfterConvertCallback.class, neumann2);
assertThat(all).containsExactly(alfred2, neumann2);
}

View File

@@ -59,6 +59,7 @@ import org.springframework.data.jdbc.testing.EnabledOnFeature;
import org.springframework.data.jdbc.testing.TestConfiguration;
import org.springframework.data.jdbc.testing.TestDatabaseFeatures;
import org.springframework.data.relational.core.mapping.event.AbstractRelationalEvent;
import org.springframework.data.relational.core.mapping.event.AfterConvertEvent;
import org.springframework.data.relational.core.mapping.event.AfterLoadEvent;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.core.NamedQueries;
@@ -309,7 +310,7 @@ public class JdbcRepositoryIntegrationTests {
repository.findAllWithSql();
assertThat(eventListener.events).hasSize(1).hasOnlyElementsOfType(AfterLoadEvent.class);
assertThat(eventListener.events).hasSize(2).hasOnlyElementsOfTypes(AfterLoadEvent.class, AfterConvertEvent.class);
}
@Test // DATAJDBC-318

View File

@@ -51,15 +51,7 @@ import org.springframework.data.relational.core.dialect.Dialect;
import org.springframework.data.relational.core.dialect.H2Dialect;
import org.springframework.data.relational.core.dialect.HsqlDbDialect;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.event.AfterDeleteEvent;
import org.springframework.data.relational.core.mapping.event.AfterLoadEvent;
import org.springframework.data.relational.core.mapping.event.AfterSaveEvent;
import org.springframework.data.relational.core.mapping.event.BeforeConvertEvent;
import org.springframework.data.relational.core.mapping.event.BeforeDeleteEvent;
import org.springframework.data.relational.core.mapping.event.BeforeSaveEvent;
import org.springframework.data.relational.core.mapping.event.Identifier;
import org.springframework.data.relational.core.mapping.event.RelationalEvent;
import org.springframework.data.relational.core.mapping.event.WithId;
import org.springframework.data.relational.core.mapping.event.*;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
@@ -198,7 +190,9 @@ public class SimpleJdbcRepositoryEventsUnitTests {
.extracting(e -> (Class) e.getClass()) //
.containsExactly( //
AfterLoadEvent.class, //
AfterLoadEvent.class //
AfterConvertEvent.class, //
AfterLoadEvent.class, //
AfterConvertEvent.class //
);
}
@@ -217,7 +211,9 @@ public class SimpleJdbcRepositoryEventsUnitTests {
.extracting(e -> (Class) e.getClass()) //
.containsExactly( //
AfterLoadEvent.class, //
AfterLoadEvent.class //
AfterConvertEvent.class, //
AfterLoadEvent.class, //
AfterConvertEvent.class //
);
}
@@ -234,7 +230,8 @@ public class SimpleJdbcRepositoryEventsUnitTests {
assertThat(publisher.events) //
.extracting(e -> (Class) e.getClass()) //
.containsExactly( //
AfterLoadEvent.class //
AfterLoadEvent.class, //
AfterConvertEvent.class //
);
}
@@ -253,7 +250,9 @@ public class SimpleJdbcRepositoryEventsUnitTests {
.extracting(e -> (Class) e.getClass()) //
.containsExactly( //
AfterLoadEvent.class, //
AfterLoadEvent.class //
AfterConvertEvent.class, //
AfterLoadEvent.class, //
AfterConvertEvent.class //
);
}
@@ -273,7 +272,9 @@ public class SimpleJdbcRepositoryEventsUnitTests {
.extracting(e -> (Class) e.getClass()) //
.containsExactly( //
AfterLoadEvent.class, //
AfterLoadEvent.class //
AfterConvertEvent.class, //
AfterLoadEvent.class, //
AfterConvertEvent.class //
);
}