DATAJDBC-233 - Polishing.
Enhance Javadoc. Fix generics for AggregateChange in JdbcAggregateTemplate. Use Lombok's Data annotation where possible. Typos. Original pull request: #80.
This commit is contained in:
@@ -38,6 +38,8 @@ public interface DataAccessStrategy {
|
||||
* @param additionalParameters name-value pairs of additional parameters. Especially ids of parent entities that need
|
||||
* to get referenced are contained in this map. Must not be {@code null}.
|
||||
* @param <T> the type of the instance.
|
||||
* @return the instance after insert into. The returned instance may be different to the given {@code instance} as
|
||||
* this method can apply changes to the return object.
|
||||
*/
|
||||
<T> T insert(T instance, Class<T> domainType, Map<String, Object> additionalParameters);
|
||||
|
||||
@@ -56,7 +58,7 @@ public interface DataAccessStrategy {
|
||||
* deletes.
|
||||
*
|
||||
* @param id the id of the row to be deleted. Must not be {@code null}.
|
||||
* @param domainType the type of entity to be deleted. Implicitely determines the table to operate on. Must not be
|
||||
* @param domainType the type of entity to be deleted. Implicitly determines the table to operate on. Must not be
|
||||
* {@code null}.
|
||||
*/
|
||||
void delete(Object id, Class<?> domainType);
|
||||
|
||||
@@ -78,6 +78,10 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
|
||||
this.accessStrategy = this;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.DataAccessStrategy#insert(java.lang.Object, java.lang.Class, java.util.Map)
|
||||
*/
|
||||
@Override
|
||||
public <T> T insert(T instance, Class<T> domainType, Map<String, Object> additionalParameters) {
|
||||
|
||||
|
||||
@@ -22,8 +22,8 @@ import org.springframework.data.relational.core.mapping.RelationalPersistentProp
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* delegates all method calls to an instance set after construction. This is useful for {@link DataAccessStrategy}s with
|
||||
* cyclical dependencies.
|
||||
* Delegates all method calls to an instance set after construction. This is useful for {@link DataAccessStrategy}s with
|
||||
* cyclic dependencies.
|
||||
*
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
@@ -31,41 +31,73 @@ public class DelegatingDataAccessStrategy implements DataAccessStrategy {
|
||||
|
||||
private DataAccessStrategy delegate;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.DataAccessStrategy#insert(java.lang.Object, java.lang.Class, java.util.Map)
|
||||
*/
|
||||
@Override
|
||||
public <T> T insert(T instance, Class<T> domainType, Map<String, Object> additionalParameters) {
|
||||
return delegate.insert(instance, domainType, additionalParameters);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.DataAccessStrategy#update(java.lang.Object, java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public <S> boolean update(S instance, Class<S> domainType) {
|
||||
return delegate.update(instance, domainType);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.DataAccessStrategy#delete(java.lang.Object, org.springframework.data.mapping.PersistentPropertyPath)
|
||||
*/
|
||||
@Override
|
||||
public void delete(Object rootId, PersistentPropertyPath<RelationalPersistentProperty> propertyPath) {
|
||||
delegate.delete(rootId, propertyPath);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.DataAccessStrategy#delete(java.lang.Object, java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public void delete(Object id, Class<?> domainType) {
|
||||
delegate.delete(id, domainType);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.DataAccessStrategy#deleteAll(java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public <T> void deleteAll(Class<T> domainType) {
|
||||
delegate.deleteAll(domainType);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.DataAccessStrategy#deleteAll(org.springframework.data.mapping.PersistentPropertyPath)
|
||||
*/
|
||||
@Override
|
||||
public void deleteAll(PersistentPropertyPath<RelationalPersistentProperty> propertyPath) {
|
||||
delegate.deleteAll(propertyPath);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.DataAccessStrategy#count(java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public long count(Class<?> domainType) {
|
||||
return delegate.count(domainType);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.DataAccessStrategy#findById(java.lang.Object, java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public <T> T findById(Object id, Class<T> domainType) {
|
||||
|
||||
@@ -74,16 +106,28 @@ public class DelegatingDataAccessStrategy implements DataAccessStrategy {
|
||||
return delegate.findById(id, domainType);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.DataAccessStrategy#findAll(java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public <T> Iterable<T> findAll(Class<T> domainType) {
|
||||
return delegate.findAll(domainType);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.DataAccessStrategy#findAllById(java.lang.Iterable, java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
|
||||
return delegate.findAllById(ids, domainType);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.DataAccessStrategy#findAllByProperty(java.lang.Object, org.springframework.data.relational.core.mapping.RelationalPersistentProperty)
|
||||
*/
|
||||
@Override
|
||||
public <T> Iterable<T> findAllByProperty(Object rootId, RelationalPersistentProperty property) {
|
||||
|
||||
@@ -92,6 +136,10 @@ public class DelegatingDataAccessStrategy implements DataAccessStrategy {
|
||||
return delegate.findAllByProperty(rootId, property);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.DataAccessStrategy#existsById(java.lang.Object, java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public <T> boolean existsById(Object id, Class<T> domainType) {
|
||||
return delegate.existsById(id, domainType);
|
||||
|
||||
@@ -26,9 +26,10 @@ public interface JdbcAggregateOperations {
|
||||
|
||||
/**
|
||||
* Saves an instance of an aggregate, including all the members of the aggregate.
|
||||
*
|
||||
*
|
||||
* @param instance the aggregate root of the aggregate to be saved. Must not be {@code null}.
|
||||
* @param <T> the type of the aggregate root.
|
||||
* @return the saved instance.
|
||||
*/
|
||||
<T> T save(T instance);
|
||||
|
||||
@@ -43,7 +44,7 @@ public interface JdbcAggregateOperations {
|
||||
|
||||
/**
|
||||
* Delete an aggregate identified by it's aggregate root.
|
||||
*
|
||||
*
|
||||
* @param aggregateRoot to delete. Must not be {@code null}.
|
||||
* @param domainType the type of the aggregate root. Must not be {@code null}.
|
||||
* @param <T> the type of the aggregate root.
|
||||
@@ -52,7 +53,7 @@ public interface JdbcAggregateOperations {
|
||||
|
||||
/**
|
||||
* Delete all aggregates of a given type.
|
||||
*
|
||||
*
|
||||
* @param domainType type of the aggregate roots to be deleted. Must not be {@code null}.
|
||||
*/
|
||||
void deleteAll(Class<?> domainType);
|
||||
@@ -97,7 +98,7 @@ public interface JdbcAggregateOperations {
|
||||
|
||||
/**
|
||||
* Checks if an aggregate identified by type and id exists in the database.
|
||||
*
|
||||
*
|
||||
* @param id the id of the aggregate root.
|
||||
* @param domainType the type of the aggregate root.
|
||||
* @param <T> the type of the aggregate root.
|
||||
|
||||
@@ -20,10 +20,10 @@ import java.util.Optional;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.data.mapping.IdentifierAccessor;
|
||||
import org.springframework.data.relational.core.conversion.AggregateChange;
|
||||
import org.springframework.data.relational.core.conversion.AggregateChange.Kind;
|
||||
import org.springframework.data.relational.core.conversion.Interpreter;
|
||||
import org.springframework.data.relational.core.conversion.RelationalEntityDeleteWriter;
|
||||
import org.springframework.data.relational.core.conversion.RelationalEntityWriter;
|
||||
import org.springframework.data.relational.core.conversion.AggregateChange.Kind;
|
||||
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
|
||||
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
|
||||
import org.springframework.data.relational.core.mapping.event.AfterDeleteEvent;
|
||||
@@ -53,18 +53,34 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
|
||||
|
||||
private final DataAccessStrategy accessStrategy;
|
||||
|
||||
/**
|
||||
* Creates a new {@link JdbcAggregateTemplate} given {@link ApplicationEventPublisher},
|
||||
* {@link RelationalMappingContext} and {@link DataAccessStrategy}.
|
||||
*
|
||||
* @param publisher must not be {@literal null}.
|
||||
* @param context must not be {@literal null}.
|
||||
* @param dataAccessStrategy must not be {@literal null}.
|
||||
*/
|
||||
public JdbcAggregateTemplate(ApplicationEventPublisher publisher, RelationalMappingContext context,
|
||||
DataAccessStrategy dataAccessStrategy) {
|
||||
|
||||
Assert.notNull(publisher, "ApplicationEventPublisher must not be null!");
|
||||
Assert.notNull(context, "RelationalMappingContext must not be null!");
|
||||
Assert.notNull(dataAccessStrategy, "DataAccessStrategy must not be null!");
|
||||
|
||||
this.publisher = publisher;
|
||||
this.context = context;
|
||||
this.accessStrategy = dataAccessStrategy;
|
||||
|
||||
this.jdbcEntityWriter = new RelationalEntityWriter(context);
|
||||
this.jdbcEntityDeleteWriter = new RelationalEntityDeleteWriter(context);
|
||||
this.accessStrategy = dataAccessStrategy;
|
||||
this.interpreter = new DefaultJdbcInterpreter(context, accessStrategy);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#save(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public <T> T save(T instance) {
|
||||
|
||||
@@ -73,7 +89,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
|
||||
RelationalPersistentEntity<?> entity = context.getRequiredPersistentEntity(instance.getClass());
|
||||
IdentifierAccessor identifierAccessor = entity.getIdentifierAccessor(instance);
|
||||
|
||||
AggregateChange change = createChange(instance);
|
||||
AggregateChange<T> change = createChange(instance);
|
||||
|
||||
publisher.publishEvent(new BeforeSaveEvent( //
|
||||
Identifier.ofNullable(identifierAccessor.getIdentifier()), //
|
||||
@@ -96,11 +112,19 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
|
||||
return (T) change.getEntity();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#count(java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public long count(Class<?> domainType) {
|
||||
return accessStrategy.count(domainType);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#findById(java.lang.Object, java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public <T> T findById(Object id, Class<T> domainType) {
|
||||
|
||||
@@ -111,11 +135,19 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
|
||||
return entity;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#existsById(java.lang.Object, java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public <T> boolean existsById(Object id, Class<T> domainType) {
|
||||
return accessStrategy.existsById(id, domainType);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#findAll(java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public <T> Iterable<T> findAll(Class<T> domainType) {
|
||||
|
||||
@@ -124,6 +156,10 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
|
||||
return all;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#findAllById(java.lang.Iterable, java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
|
||||
|
||||
@@ -132,6 +168,10 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
|
||||
return allById;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#delete(java.lang.Object, java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public <S> void delete(S aggregateRoot, Class<S> domainType) {
|
||||
|
||||
@@ -141,21 +181,29 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
|
||||
deleteTree(identifierAccessor.getRequiredIdentifier(), aggregateRoot, domainType);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#deleteById(java.lang.Object, java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public <S> void deleteById(Object id, Class<S> domainType) {
|
||||
deleteTree(id, null, domainType);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#deleteAll(java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public void deleteAll(Class<?> domainType) {
|
||||
|
||||
AggregateChange change = createDeletingChange(domainType);
|
||||
AggregateChange<?> change = createDeletingChange(domainType);
|
||||
change.executeWith(interpreter);
|
||||
}
|
||||
|
||||
private void deleteTree(Object id, @Nullable Object entity, Class<?> domainType) {
|
||||
|
||||
AggregateChange change = createDeletingChange(id, entity, domainType);
|
||||
AggregateChange<?> change = createDeletingChange(id, entity, domainType);
|
||||
|
||||
Specified specifiedId = Identifier.of(id);
|
||||
Optional<Object> optionalEntity = Optional.ofNullable(entity);
|
||||
@@ -166,23 +214,23 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
|
||||
publisher.publishEvent(new AfterDeleteEvent(specifiedId, optionalEntity, change));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> AggregateChange createChange(T instance) {
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
private <T> AggregateChange<T> createChange(T instance) {
|
||||
|
||||
AggregateChange<?> aggregateChange = new AggregateChange(Kind.SAVE, instance.getClass(), instance);
|
||||
AggregateChange<T> aggregateChange = new AggregateChange(Kind.SAVE, instance.getClass(), instance);
|
||||
jdbcEntityWriter.write(instance, aggregateChange);
|
||||
return aggregateChange;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private AggregateChange createDeletingChange(Object id, @Nullable Object entity, Class<?> domainType) {
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
private AggregateChange<?> createDeletingChange(Object id, @Nullable Object entity, Class<?> domainType) {
|
||||
|
||||
AggregateChange<?> aggregateChange = new AggregateChange(Kind.DELETE, domainType, entity);
|
||||
jdbcEntityDeleteWriter.write(id, aggregateChange);
|
||||
return aggregateChange;
|
||||
}
|
||||
|
||||
private AggregateChange createDeletingChange(Class<?> domainType) {
|
||||
private AggregateChange<?> createDeletingChange(Class<?> domainType) {
|
||||
|
||||
AggregateChange<?> aggregateChange = new AggregateChange<>(Kind.DELETE, domainType, null);
|
||||
jdbcEntityDeleteWriter.write(null, aggregateChange);
|
||||
|
||||
@@ -15,11 +15,9 @@
|
||||
*/
|
||||
package org.springframework.data.relational.core.conversion;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Data;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.Value;
|
||||
|
||||
import java.util.HashMap;
|
||||
@@ -34,6 +32,7 @@ import org.springframework.data.relational.core.mapping.RelationalPersistentProp
|
||||
*
|
||||
* @param <T> the type of the entity that is affected by this action.
|
||||
* @author Jens Schauder
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public interface DbAction<T> {
|
||||
|
||||
@@ -68,9 +67,7 @@ public interface DbAction<T> {
|
||||
*
|
||||
* @param <T> type of the entity for which this represents a database interaction.
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@Data
|
||||
@RequiredArgsConstructor
|
||||
class Insert<T> implements WithDependingOn<T>, WithEntity<T>, WithResultEntity<T> {
|
||||
|
||||
@@ -98,9 +95,7 @@ public interface DbAction<T> {
|
||||
*
|
||||
* @param <T> type of the entity for which this represents a database interaction.
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@Data
|
||||
@RequiredArgsConstructor
|
||||
class InsertRoot<T> implements WithEntity<T>, WithResultEntity<T> {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user