Skip to content

Commit 464782c

Browse files
committed
DATAJDBC-197 - Repositories find methods now emit AfterCreation events as intended.
Event publishing moved into the JdbcEntityTemplate in order to ensure events for AggregateRoots. Removed superfluous AggregateChange from AfterCreation event.
1 parent 8afdaf5 commit 464782c

File tree

5 files changed

+88
-138
lines changed

5 files changed

+88
-138
lines changed

src/main/java/org/springframework/data/jdbc/core/EventPublishingEntityRowMapper.java

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/main/java/org/springframework/data/jdbc/core/JdbcEntityTemplate.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.data.jdbc.core.conversion.Interpreter;
2424
import org.springframework.data.jdbc.core.conversion.JdbcEntityDeleteWriter;
2525
import org.springframework.data.jdbc.core.conversion.JdbcEntityWriter;
26+
import org.springframework.data.jdbc.mapping.event.AfterCreation;
2627
import org.springframework.data.jdbc.mapping.event.AfterDelete;
2728
import org.springframework.data.jdbc.mapping.event.AfterSave;
2829
import org.springframework.data.jdbc.mapping.event.BeforeDelete;
@@ -90,7 +91,12 @@ public long count(Class<?> domainType) {
9091

9192
@Override
9293
public <T> T findById(Object id, Class<T> domainType) {
93-
return accessStrategy.findById(id, domainType);
94+
95+
T entity = accessStrategy.findById(id, domainType);
96+
if (entity != null) {
97+
publishAfterCreation(id, entity);
98+
}
99+
return entity;
94100
}
95101

96102
@Override
@@ -100,12 +106,18 @@ public <T> boolean existsById(Object id, Class<T> domainType) {
100106

101107
@Override
102108
public <T> Iterable<T> findAll(Class<T> domainType) {
103-
return accessStrategy.findAll(domainType);
109+
110+
Iterable<T> all = accessStrategy.findAll(domainType);
111+
publishAfterCreation(all);
112+
return all;
104113
}
105114

106115
@Override
107116
public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
108-
return accessStrategy.findAllById(ids, domainType);
117+
118+
Iterable<T> allById = accessStrategy.findAllById(ids, domainType);
119+
publishAfterCreation(allById);
120+
return allById;
109121
}
110122

111123
@Override
@@ -161,4 +173,15 @@ private AggregateChange createDeletingChange(Class<?> domainType) {
161173
jdbcEntityDeleteWriter.write(null, aggregateChange);
162174
return aggregateChange;
163175
}
176+
177+
private <T> void publishAfterCreation(Iterable<T> all) {
178+
179+
all.forEach(e -> {
180+
publishAfterCreation(context.getRequiredPersistentEntityInformation((Class<T>) e.getClass()).getRequiredId(e), e);
181+
});
182+
}
183+
184+
private <T> void publishAfterCreation(Object id, T entity) {
185+
publisher.publishEvent(new AfterCreation(Identifier.of(id), entity));
186+
}
164187
}

src/main/java/org/springframework/data/jdbc/mapping/event/AfterCreation.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.jdbc.mapping.event;
1717

18-
import org.springframework.data.jdbc.core.conversion.AggregateChange;
1918
import org.springframework.data.jdbc.mapping.event.Identifier.Specified;
2019

2120
/**
@@ -32,9 +31,8 @@ public class AfterCreation extends JdbcEventWithIdAndEntity {
3231
/**
3332
* @param id of the entity
3433
* @param entity the newly instantiated entity.
35-
* @param change
3634
*/
37-
public AfterCreation(Specified id, Object entity, AggregateChange change) {
38-
super(id, entity, change);
35+
public AfterCreation(Specified id, Object entity) {
36+
super(id, entity, null);
3937
}
4038
}

src/test/java/org/springframework/data/jdbc/core/EventPublishingEntityRowMapperUnitTests.java

Lines changed: 0 additions & 70 deletions
This file was deleted.

src/test/java/org/springframework/data/jdbc/repository/SimpleJdbcRepositoryEventsUnitTests.java

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.data.annotation.Id;
2222
import org.springframework.data.jdbc.core.DefaultDataAccessStrategy;
2323
import org.springframework.data.jdbc.core.SqlGeneratorSource;
24+
import org.springframework.data.jdbc.mapping.event.AfterCreation;
2425
import org.springframework.data.jdbc.mapping.event.AfterDelete;
2526
import org.springframework.data.jdbc.mapping.event.AfterSave;
2627
import org.springframework.data.jdbc.mapping.event.BeforeDelete;
@@ -42,18 +43,22 @@ public class SimpleJdbcRepositoryEventsUnitTests {
4243
FakePublisher publisher = new FakePublisher();
4344

4445
DummyEntityRepository repository;
46+
DefaultDataAccessStrategy dataAccessStrategy;
4547

4648
@Before
4749
public void before() {
4850

4951
final JdbcMappingContext context = new JdbcMappingContext(createIdGeneratingOperations());
52+
53+
dataAccessStrategy = spy(new DefaultDataAccessStrategy( //
54+
new SqlGeneratorSource(context), //
55+
context //
56+
));
57+
5058
JdbcRepositoryFactory factory = new JdbcRepositoryFactory( //
5159
publisher, //
5260
context, //
53-
new DefaultDataAccessStrategy( //
54-
new SqlGeneratorSource(context), //
55-
context //
56-
) //
61+
dataAccessStrategy //
5762
);
5863

5964
repository = factory.getRepository(DummyEntityRepository.class);
@@ -122,6 +127,57 @@ public void publishesEventsOnDeleteById() {
122127
);
123128
}
124129

130+
@Test // DATAJDBC-197
131+
public void publishesEventsOnFindAll() {
132+
133+
DummyEntity entity1 = new DummyEntity(42L);
134+
DummyEntity entity2 = new DummyEntity(23L);
135+
136+
doReturn(asList(entity1, entity2)).when(dataAccessStrategy).findAll(any(Class.class));
137+
138+
repository.findAll();
139+
140+
assertThat(publisher.events) //
141+
.extracting(e -> (Class) e.getClass()) //
142+
.containsExactly( //
143+
AfterCreation.class, //
144+
AfterCreation.class //
145+
);
146+
}
147+
148+
@Test // DATAJDBC-197
149+
public void publishesEventsOnFindAllById() {
150+
151+
DummyEntity entity1 = new DummyEntity(42L);
152+
DummyEntity entity2 = new DummyEntity(23L);
153+
154+
doReturn(asList(entity1, entity2)).when(dataAccessStrategy).findAllById(any(Iterable.class), any(Class.class));
155+
156+
repository.findAllById(asList(42L, 23L));
157+
158+
assertThat(publisher.events) //
159+
.extracting(e -> (Class) e.getClass()) //
160+
.containsExactly( //
161+
AfterCreation.class, //
162+
AfterCreation.class //
163+
);
164+
}
165+
166+
@Test // DATAJDBC-197
167+
public void publishesEventsOnFindById() {
168+
169+
DummyEntity entity1 = new DummyEntity(23L);
170+
doReturn(entity1).when(dataAccessStrategy).findById(eq(23L), any(Class.class));
171+
172+
repository.findById(23L);
173+
174+
assertThat(publisher.events) //
175+
.extracting(e -> (Class) e.getClass()) //
176+
.containsExactly( //
177+
AfterCreation.class //
178+
);
179+
}
180+
125181
private static NamedParameterJdbcOperations createIdGeneratingOperations() {
126182

127183
Answer<Integer> setIdInKeyHolder = invocation -> {

0 commit comments

Comments
 (0)