diff --git a/src/main/java/org/springframework/data/jdbc/core/DataAccessStrategy.java b/src/main/java/org/springframework/data/jdbc/core/DataAccessStrategy.java index 562404253..0be96173b 100644 --- a/src/main/java/org/springframework/data/jdbc/core/DataAccessStrategy.java +++ b/src/main/java/org/springframework/data/jdbc/core/DataAccessStrategy.java @@ -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 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 insert(T instance, Class domainType, Map 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); diff --git a/src/main/java/org/springframework/data/jdbc/core/DefaultDataAccessStrategy.java b/src/main/java/org/springframework/data/jdbc/core/DefaultDataAccessStrategy.java index 0a04a514a..931e8418f 100644 --- a/src/main/java/org/springframework/data/jdbc/core/DefaultDataAccessStrategy.java +++ b/src/main/java/org/springframework/data/jdbc/core/DefaultDataAccessStrategy.java @@ -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 insert(T instance, Class domainType, Map additionalParameters) { diff --git a/src/main/java/org/springframework/data/jdbc/core/DelegatingDataAccessStrategy.java b/src/main/java/org/springframework/data/jdbc/core/DelegatingDataAccessStrategy.java index 6ffea6c64..7665196e1 100644 --- a/src/main/java/org/springframework/data/jdbc/core/DelegatingDataAccessStrategy.java +++ b/src/main/java/org/springframework/data/jdbc/core/DelegatingDataAccessStrategy.java @@ -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 insert(T instance, Class domainType, Map 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 boolean update(S instance, Class 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 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 void deleteAll(Class domainType) { delegate.deleteAll(domainType); } + /* + * (non-Javadoc) + * @see org.springframework.data.jdbc.core.DataAccessStrategy#deleteAll(org.springframework.data.mapping.PersistentPropertyPath) + */ @Override public void deleteAll(PersistentPropertyPath 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 findById(Object id, Class 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 Iterable findAll(Class domainType) { return delegate.findAll(domainType); } + /* + * (non-Javadoc) + * @see org.springframework.data.jdbc.core.DataAccessStrategy#findAllById(java.lang.Iterable, java.lang.Class) + */ @Override public Iterable findAllById(Iterable ids, Class 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 Iterable 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 boolean existsById(Object id, Class domainType) { return delegate.existsById(id, domainType); diff --git a/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateOperations.java b/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateOperations.java index af3026b1b..69663bb99 100644 --- a/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateOperations.java +++ b/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateOperations.java @@ -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 the type of the aggregate root. + * @return the saved instance. */ 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 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 the type of the aggregate root. diff --git a/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java b/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java index 5ef66c5ed..c28aa135f 100644 --- a/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java +++ b/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java @@ -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 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 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 findById(Object id, Class 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 boolean existsById(Object id, Class domainType) { return accessStrategy.existsById(id, domainType); } + /* + * (non-Javadoc) + * @see org.springframework.data.jdbc.core.JdbcAggregateOperations#findAll(java.lang.Class) + */ @Override public Iterable findAll(Class 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 Iterable findAllById(Iterable ids, Class 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 void delete(S aggregateRoot, Class 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 void deleteById(Object id, Class 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 optionalEntity = Optional.ofNullable(entity); @@ -166,23 +214,23 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations { publisher.publishEvent(new AfterDeleteEvent(specifiedId, optionalEntity, change)); } - @SuppressWarnings("unchecked") - private AggregateChange createChange(T instance) { + @SuppressWarnings({ "unchecked", "rawtypes" }) + private AggregateChange createChange(T instance) { - AggregateChange aggregateChange = new AggregateChange(Kind.SAVE, instance.getClass(), instance); + AggregateChange 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); diff --git a/src/main/java/org/springframework/data/relational/core/conversion/DbAction.java b/src/main/java/org/springframework/data/relational/core/conversion/DbAction.java index 5a897dab8..d69843a93 100644 --- a/src/main/java/org/springframework/data/relational/core/conversion/DbAction.java +++ b/src/main/java/org/springframework/data/relational/core/conversion/DbAction.java @@ -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 the type of the entity that is affected by this action. * @author Jens Schauder + * @author Mark Paluch */ public interface DbAction { @@ -68,9 +67,7 @@ public interface DbAction { * * @param type of the entity for which this represents a database interaction. */ - @Getter - @Setter - @ToString + @Data @RequiredArgsConstructor class Insert implements WithDependingOn, WithEntity, WithResultEntity { @@ -98,9 +95,7 @@ public interface DbAction { * * @param type of the entity for which this represents a database interaction. */ - @Getter - @Setter - @ToString + @Data @RequiredArgsConstructor class InsertRoot implements WithEntity, WithResultEntity {