DATAJDBC-197 - Repositories find methods now emit AfterLoadEvents as intended.
Event publishing moved into the JdbcEntityTemplate in order to ensure events for AggregateRoots. Removed superfluous AggregateChange from AfterCreation event. Original pull request: #58.
This commit is contained in:
committed by
Mark Paluch
parent
d4b2eca43a
commit
fbac3a5529
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
* Copyright 2017-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.jdbc.core;
|
||||
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.data.jdbc.mapping.event.AfterCreation;
|
||||
import org.springframework.data.jdbc.mapping.event.Identifier;
|
||||
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntityInformation;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
/**
|
||||
* A {@link RowMapper} that publishes events after a delegate, did the actual work of mapping a {@link ResultSet} to an
|
||||
* entityInformation.
|
||||
*
|
||||
* @author Jens Schauder
|
||||
* @since 2.0
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class EventPublishingEntityRowMapper<T> implements RowMapper<T> {
|
||||
|
||||
private final @NonNull RowMapper<T> delegate;
|
||||
private final @NonNull JdbcPersistentEntityInformation<T, ?> entityInformation;
|
||||
private final @NonNull ApplicationEventPublisher publisher;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.jdbc.core.RowMapper#mapRow(java.sql.ResultSet, int)
|
||||
*/
|
||||
@Override
|
||||
public T mapRow(ResultSet resultSet, int i) throws SQLException {
|
||||
|
||||
T instance = delegate.mapRow(resultSet, i);
|
||||
|
||||
publisher.publishEvent(new AfterCreation(Identifier.of(entityInformation.getRequiredId(instance)), instance, null));
|
||||
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import org.springframework.data.jdbc.core.conversion.AggregateChange.Kind;
|
||||
import org.springframework.data.jdbc.core.conversion.Interpreter;
|
||||
import org.springframework.data.jdbc.core.conversion.JdbcEntityDeleteWriter;
|
||||
import org.springframework.data.jdbc.core.conversion.JdbcEntityWriter;
|
||||
import org.springframework.data.jdbc.mapping.event.AfterCreation;
|
||||
import org.springframework.data.jdbc.mapping.event.AfterDelete;
|
||||
import org.springframework.data.jdbc.mapping.event.AfterSave;
|
||||
import org.springframework.data.jdbc.mapping.event.BeforeDelete;
|
||||
@@ -90,7 +91,12 @@ public class JdbcEntityTemplate implements JdbcEntityOperations {
|
||||
|
||||
@Override
|
||||
public <T> T findById(Object id, Class<T> domainType) {
|
||||
return accessStrategy.findById(id, domainType);
|
||||
|
||||
T entity = accessStrategy.findById(id, domainType);
|
||||
if (entity != null) {
|
||||
publishAfterCreation(id, entity);
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -100,12 +106,18 @@ public class JdbcEntityTemplate implements JdbcEntityOperations {
|
||||
|
||||
@Override
|
||||
public <T> Iterable<T> findAll(Class<T> domainType) {
|
||||
return accessStrategy.findAll(domainType);
|
||||
|
||||
Iterable<T> all = accessStrategy.findAll(domainType);
|
||||
publishAfterCreation(all);
|
||||
return all;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
|
||||
return accessStrategy.findAllById(ids, domainType);
|
||||
|
||||
Iterable<T> allById = accessStrategy.findAllById(ids, domainType);
|
||||
publishAfterCreation(allById);
|
||||
return allById;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -161,4 +173,15 @@ public class JdbcEntityTemplate implements JdbcEntityOperations {
|
||||
jdbcEntityDeleteWriter.write(null, aggregateChange);
|
||||
return aggregateChange;
|
||||
}
|
||||
|
||||
private <T> void publishAfterCreation(Iterable<T> all) {
|
||||
|
||||
all.forEach(e -> {
|
||||
publishAfterCreation(context.getRequiredPersistentEntityInformation((Class<T>) e.getClass()).getRequiredId(e), e);
|
||||
});
|
||||
}
|
||||
|
||||
private <T> void publishAfterCreation(Object id, T entity) {
|
||||
publisher.publishEvent(new AfterCreation(Identifier.of(id), entity));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package org.springframework.data.jdbc.mapping.event;
|
||||
|
||||
import org.springframework.data.jdbc.core.conversion.AggregateChange;
|
||||
import org.springframework.data.jdbc.mapping.event.Identifier.Specified;
|
||||
|
||||
/**
|
||||
@@ -32,9 +31,8 @@ public class AfterCreation extends JdbcEventWithIdAndEntity {
|
||||
/**
|
||||
* @param id of the entity
|
||||
* @param entity the newly instantiated entity.
|
||||
* @param change
|
||||
*/
|
||||
public AfterCreation(Specified id, Object entity, AggregateChange change) {
|
||||
super(id, entity, change);
|
||||
public AfterCreation(Specified id, Object entity) {
|
||||
super(id, entity, null);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user