Browse Source

DATAJDBC-282 - Add insert and update methods to JdbcRepository.

This introduces the JdbcRepository interface which offers two additional methods beyond the CrudRepository:
`insert` and `update`.
Both methods skip the test if the aggregate is new and perform the respective operation.

Especially `insert` is useful for saving new aggregates which are new but have an ID set by the client and not generated by the database.

Original pull request: #107.
pull/108/head
Thomas Lang 7 years ago committed by Jens Schauder
parent
commit
d049988028
  1. 18
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateOperations.java
  2. 118
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java
  3. 18
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcRepository.java
  4. 27
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/SimpleJdbcRepository.java
  5. 48
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java
  6. 274
      spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/AbstractRelationalEntityWriter.java
  7. 44
      spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/RelationalEntityInsertWriter.java
  8. 44
      spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/RelationalEntityUpdateWriter.java
  9. 232
      spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/RelationalEntityWriter.java
  10. 111
      spring-data-relational/src/test/java/org/springframework/data/relational/core/conversion/RelationalEntityInsertWriterUnitTests.java
  11. 96
      spring-data-relational/src/test/java/org/springframework/data/relational/core/conversion/RelationalEntityUpdateWriterUnitTests.java

18
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateOperations.java

@ -33,6 +33,24 @@ public interface JdbcAggregateOperations {
*/ */
<T> T save(T instance); <T> T save(T instance);
/**
* Dedicated insert function to do just the insert of an instance of an aggregate, including all the members of the aggregate.
*
* @param instance the aggregate root of the aggregate to be inserted. Must not be {@code null}.
* @param <T> the type of the aggregate root.
* @return the saved instance.
*/
<T> T insert(T instance);
/**
* Dedicated update function to do just an update of an instance of an aggregate, including all the members of the aggregate.
*
* @param instance the aggregate root of the aggregate to be inserted. Must not be {@code null}.
* @param <T> the type of the aggregate root.
* @return the saved instance.
*/
<T> T update(T instance);
/** /**
* Deletes a single Aggregate including all entities contained in that aggregate. * Deletes a single Aggregate including all entities contained in that aggregate.
* *

118
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java

@ -24,6 +24,8 @@ import org.springframework.data.relational.core.conversion.AggregateChange.Kind;
import org.springframework.data.relational.core.conversion.Interpreter; import org.springframework.data.relational.core.conversion.Interpreter;
import org.springframework.data.relational.core.conversion.RelationalConverter; import org.springframework.data.relational.core.conversion.RelationalConverter;
import org.springframework.data.relational.core.conversion.RelationalEntityDeleteWriter; import org.springframework.data.relational.core.conversion.RelationalEntityDeleteWriter;
import org.springframework.data.relational.core.conversion.RelationalEntityInsertWriter;
import org.springframework.data.relational.core.conversion.RelationalEntityUpdateWriter;
import org.springframework.data.relational.core.conversion.RelationalEntityWriter; import org.springframework.data.relational.core.conversion.RelationalEntityWriter;
import org.springframework.data.relational.core.mapping.RelationalMappingContext; import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity; import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
@ -52,9 +54,35 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
private final RelationalEntityWriter jdbcEntityWriter; private final RelationalEntityWriter jdbcEntityWriter;
private final RelationalEntityDeleteWriter jdbcEntityDeleteWriter; private final RelationalEntityDeleteWriter jdbcEntityDeleteWriter;
private final RelationalEntityInsertWriter jdbcEntityInsertWriter;
private final RelationalEntityUpdateWriter jdbcEntityUpdateWriter;
private final DataAccessStrategy accessStrategy; private final DataAccessStrategy accessStrategy;
private <T> T store(T instance, IdentifierAccessor identifierAccessor, AggregateChange<T> change,
RelationalPersistentEntity<?> persistentEntity) {
Assert.notNull(instance, "Aggregate instance must not be null!");
publisher.publishEvent(new BeforeSaveEvent( //
Identifier.ofNullable(identifierAccessor.getIdentifier()), //
instance, //
change //
));
change.executeWith(interpreter, context, converter);
Object identifier = persistentEntity.getIdentifierAccessor(change.getEntity()).getIdentifier();
Assert.notNull(identifier, "After saving the identifier must not be null");
publisher.publishEvent(new AfterSaveEvent( //
Identifier.of(identifier), //
change.getEntity(), //
change //
));
return (T) change.getEntity();
}
/** /**
* Creates a new {@link JdbcAggregateTemplate} given {@link ApplicationEventPublisher}, * Creates a new {@link JdbcAggregateTemplate} given {@link ApplicationEventPublisher},
* {@link RelationalMappingContext} and {@link DataAccessStrategy}. * {@link RelationalMappingContext} and {@link DataAccessStrategy}.
@ -77,6 +105,8 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
this.accessStrategy = dataAccessStrategy; this.accessStrategy = dataAccessStrategy;
this.jdbcEntityWriter = new RelationalEntityWriter(context); this.jdbcEntityWriter = new RelationalEntityWriter(context);
this.jdbcEntityInsertWriter = new RelationalEntityInsertWriter(context);
this.jdbcEntityUpdateWriter = new RelationalEntityUpdateWriter(context);
this.jdbcEntityDeleteWriter = new RelationalEntityDeleteWriter(context); this.jdbcEntityDeleteWriter = new RelationalEntityDeleteWriter(context);
this.interpreter = new DefaultJdbcInterpreter(context, accessStrategy); this.interpreter = new DefaultJdbcInterpreter(context, accessStrategy);
} }
@ -85,8 +115,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#save(java.lang.Object) * @see org.springframework.data.jdbc.core.JdbcAggregateOperations#save(java.lang.Object)
*/ */
@Override @Override public <T> T save(T instance) {
public <T> T save(T instance) {
Assert.notNull(instance, "Aggregate instance must not be null!"); Assert.notNull(instance, "Aggregate instance must not be null!");
@ -95,33 +124,48 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
AggregateChange<T> change = createChange(instance); AggregateChange<T> change = createChange(instance);
publisher.publishEvent(new BeforeSaveEvent( // return store(instance, identifierAccessor, change, persistentEntity);
Identifier.ofNullable(identifierAccessor.getIdentifier()), // }
instance, //
change //
));
change.executeWith(interpreter, context, converter); /**
* Dedicated insert function to do just the insert of an instance of an aggregate, including all the members of the aggregate.
*
* @param instance the aggregate root of the aggregate to be inserted. Must not be {@code null}.
* @return the saved instance.
*/
@Override public <T> T insert(T instance) {
Assert.notNull(instance, "Aggregate instance must not be null!");
Object identifier = persistentEntity.getIdentifierAccessor(change.getEntity()).getIdentifier(); RelationalPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(instance.getClass());
IdentifierAccessor identifierAccessor = persistentEntity.getIdentifierAccessor(instance);
Assert.notNull(identifier, "After saving the identifier must not be null"); AggregateChange<T> change = createInsertChange(instance);
publisher.publishEvent(new AfterSaveEvent( // return store(instance, identifierAccessor, change, persistentEntity);
Identifier.of(identifier), // }
change.getEntity(), //
change //
));
return (T) change.getEntity(); /**
* Dedicated update function to do just an update of an instance of an aggregate, including all the members of the aggregate.
*
* @param instance the aggregate root of the aggregate to be inserted. Must not be {@code null}.
* @return the saved instance.
*/
@Override public <T> T update(T instance) {
Assert.notNull(instance, "Aggregate instance must not be null!");
RelationalPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(instance.getClass());
IdentifierAccessor identifierAccessor = persistentEntity.getIdentifierAccessor(instance);
AggregateChange<T> change = createUpdateChange(instance);
return store(instance, identifierAccessor, change, persistentEntity);
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#count(java.lang.Class) * @see org.springframework.data.jdbc.core.JdbcAggregateOperations#count(java.lang.Class)
*/ */
@Override @Override public long count(Class<?> domainType) {
public long count(Class<?> domainType) {
return accessStrategy.count(domainType); return accessStrategy.count(domainType);
} }
@ -129,8 +173,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#findById(java.lang.Object, java.lang.Class) * @see org.springframework.data.jdbc.core.JdbcAggregateOperations#findById(java.lang.Object, java.lang.Class)
*/ */
@Override @Override public <T> T findById(Object id, Class<T> domainType) {
public <T> T findById(Object id, Class<T> domainType) {
T entity = accessStrategy.findById(id, domainType); T entity = accessStrategy.findById(id, domainType);
if (entity != null) { if (entity != null) {
@ -143,8 +186,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#existsById(java.lang.Object, java.lang.Class) * @see org.springframework.data.jdbc.core.JdbcAggregateOperations#existsById(java.lang.Object, java.lang.Class)
*/ */
@Override @Override public <T> boolean existsById(Object id, Class<T> domainType) {
public <T> boolean existsById(Object id, Class<T> domainType) {
return accessStrategy.existsById(id, domainType); return accessStrategy.existsById(id, domainType);
} }
@ -152,8 +194,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#findAll(java.lang.Class) * @see org.springframework.data.jdbc.core.JdbcAggregateOperations#findAll(java.lang.Class)
*/ */
@Override @Override public <T> Iterable<T> findAll(Class<T> domainType) {
public <T> Iterable<T> findAll(Class<T> domainType) {
Iterable<T> all = accessStrategy.findAll(domainType); Iterable<T> all = accessStrategy.findAll(domainType);
publishAfterLoad(all); publishAfterLoad(all);
@ -164,8 +205,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#findAllById(java.lang.Iterable, java.lang.Class) * @see org.springframework.data.jdbc.core.JdbcAggregateOperations#findAllById(java.lang.Iterable, java.lang.Class)
*/ */
@Override @Override public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
Iterable<T> allById = accessStrategy.findAllById(ids, domainType); Iterable<T> allById = accessStrategy.findAllById(ids, domainType);
publishAfterLoad(allById); publishAfterLoad(allById);
@ -176,8 +216,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#delete(java.lang.Object, java.lang.Class) * @see org.springframework.data.jdbc.core.JdbcAggregateOperations#delete(java.lang.Object, java.lang.Class)
*/ */
@Override @Override public <S> void delete(S aggregateRoot, Class<S> domainType) {
public <S> void delete(S aggregateRoot, Class<S> domainType) {
IdentifierAccessor identifierAccessor = context.getRequiredPersistentEntity(domainType) IdentifierAccessor identifierAccessor = context.getRequiredPersistentEntity(domainType)
.getIdentifierAccessor(aggregateRoot); .getIdentifierAccessor(aggregateRoot);
@ -189,8 +228,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#deleteById(java.lang.Object, java.lang.Class) * @see org.springframework.data.jdbc.core.JdbcAggregateOperations#deleteById(java.lang.Object, java.lang.Class)
*/ */
@Override @Override public <S> void deleteById(Object id, Class<S> domainType) {
public <S> void deleteById(Object id, Class<S> domainType) {
deleteTree(id, null, domainType); deleteTree(id, null, domainType);
} }
@ -198,8 +236,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#deleteAll(java.lang.Class) * @see org.springframework.data.jdbc.core.JdbcAggregateOperations#deleteAll(java.lang.Class)
*/ */
@Override @Override public void deleteAll(Class<?> domainType) {
public void deleteAll(Class<?> domainType) {
AggregateChange<?> change = createDeletingChange(domainType); AggregateChange<?> change = createDeletingChange(domainType);
change.executeWith(interpreter, context, converter); change.executeWith(interpreter, context, converter);
@ -218,14 +255,27 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
publisher.publishEvent(new AfterDeleteEvent(specifiedId, optionalEntity, change)); publisher.publishEvent(new AfterDeleteEvent(specifiedId, optionalEntity, change));
} }
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings({ "unchecked", "rawtypes" }) private <T> AggregateChange<T> createChange(T instance) {
private <T> AggregateChange<T> createChange(T instance) {
AggregateChange<T> aggregateChange = new AggregateChange(Kind.SAVE, instance.getClass(), instance); AggregateChange<T> aggregateChange = new AggregateChange(Kind.SAVE, instance.getClass(), instance);
jdbcEntityWriter.write(instance, aggregateChange); jdbcEntityWriter.write(instance, aggregateChange);
return aggregateChange; return aggregateChange;
} }
@SuppressWarnings({ "unchecked", "rawtypes" }) private <T> AggregateChange<T> createInsertChange(T instance) {
AggregateChange<T> aggregateChange = new AggregateChange(Kind.SAVE, instance.getClass(), instance);
jdbcEntityInsertWriter.write(instance, aggregateChange);
return aggregateChange;
}
@SuppressWarnings({ "unchecked", "rawtypes" }) private <T> AggregateChange<T> createUpdateChange(T instance) {
AggregateChange<T> aggregateChange = new AggregateChange(Kind.SAVE, instance.getClass(), instance);
jdbcEntityUpdateWriter.write(instance, aggregateChange);
return aggregateChange;
}
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings({ "unchecked", "rawtypes" })
private AggregateChange<?> createDeletingChange(Object id, @Nullable Object entity, Class<?> domainType) { private AggregateChange<?> createDeletingChange(Object id, @Nullable Object entity, Class<?> domainType) {

18
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcRepository.java

@ -0,0 +1,18 @@
package org.springframework.data.jdbc.core;
import org.springframework.data.repository.Repository;
/**
* Jdbc repository for dedicated insert(), update() and upsert() sql functions.
* Other than {@link org.springframework.data.jdbc.repository.support.SimpleJdbcRepository}
* there should be bypassing of the isNew check.
*
* @author Thomas Lang
* @see <a href="https://jira.spring.io/browse/DATAJDBC-282">DATAJDBC-282</a>
*/
public interface JdbcRepository<T, ID> extends Repository<T, ID> {
<S extends T> S insert(S var1);
<S extends T> S update(S var1);
}

27
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/SimpleJdbcRepository.java

@ -22,6 +22,7 @@ import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.springframework.data.jdbc.core.JdbcAggregateOperations; import org.springframework.data.jdbc.core.JdbcAggregateOperations;
import org.springframework.data.jdbc.core.JdbcRepository;
import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.data.util.Streamable; import org.springframework.data.util.Streamable;
@ -31,10 +32,12 @@ import org.springframework.data.util.Streamable;
* @author Oliver Gierke * @author Oliver Gierke
*/ */
@RequiredArgsConstructor @RequiredArgsConstructor
public class SimpleJdbcRepository<T, ID> implements CrudRepository<T, ID> { public class SimpleJdbcRepository<T, ID> implements CrudRepository<T, ID>, JdbcRepository<T, ID> {
private final @NonNull JdbcAggregateOperations entityOperations; private final @NonNull
private final @NonNull PersistentEntity<T, ?> entity; JdbcAggregateOperations entityOperations;
private final @NonNull
PersistentEntity<T, ?> entity;
/* /*
* (non-Javadoc) * (non-Javadoc)
@ -134,4 +137,22 @@ public class SimpleJdbcRepository<T, ID> implements CrudRepository<T, ID> {
public void deleteAll() { public void deleteAll() {
entityOperations.deleteAll(entity.getType()); entityOperations.deleteAll(entity.getType());
} }
/*
* (non-Javadoc)
* @see org.springframework.data.repository.JdbcRepository#insert(T t)
*/
@Override
public <S extends T> S insert(S var1) {
return entityOperations.insert(var1);
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.JdbcRepository#update(T t)
*/
@Override
public <S extends T> S update(S var1) {
return entityOperations.update(var1);
}
} }

48
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java

@ -28,6 +28,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.jdbc.core.JdbcRepository;
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory; import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory;
import org.springframework.data.jdbc.testing.TestConfiguration; import org.springframework.data.jdbc.testing.TestConfiguration;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
@ -52,7 +53,8 @@ public class JdbcRepositoryIntegrationTests {
@Import(TestConfiguration.class) @Import(TestConfiguration.class)
static class Config { static class Config {
@Autowired JdbcRepositoryFactory factory; @Autowired
JdbcRepositoryFactory factory;
@Bean @Bean
Class<?> testClass() { Class<?> testClass() {
@ -66,11 +68,15 @@ public class JdbcRepositoryIntegrationTests {
} }
@ClassRule public static final SpringClassRule classRule = new SpringClassRule(); @ClassRule
@Rule public SpringMethodRule methodRule = new SpringMethodRule(); public static final SpringClassRule classRule = new SpringClassRule();
@Rule
public SpringMethodRule methodRule = new SpringMethodRule();
@Autowired NamedParameterJdbcTemplate template; @Autowired
@Autowired DummyEntityRepository repository; NamedParameterJdbcTemplate template;
@Autowired
DummyEntityRepository repository;
@Test // DATAJDBC-95 @Test // DATAJDBC-95
public void savesAnEntity() { public void savesAnEntity() {
@ -81,6 +87,24 @@ public class JdbcRepositoryIntegrationTests {
"id_Prop = " + entity.getIdProp())).isEqualTo(1); "id_Prop = " + entity.getIdProp())).isEqualTo(1);
} }
@Test // DATAJDBC-282
public void insertAnEntity() {
DummyEntity entity = repository.insert(createDummyEntity());
assertThat(JdbcTestUtils.countRowsInTableWhere((JdbcTemplate) template.getJdbcOperations(), "dummy_entity",
"id_Prop = " + entity.getIdProp())).isEqualTo(1);
}
@Test // DATAJDBC-282
public void insertAnExistingEntity() {
DummyEntity existingDummyEntity = createExistingDummyEntity();
DummyEntity entity = repository.insert(existingDummyEntity);
assertThat(JdbcTestUtils.countRowsInTableWhere((JdbcTemplate) template.getJdbcOperations(), "dummy_entity",
"id_Prop = " + existingDummyEntity.getIdProp())).isEqualTo(1);
}
@Test // DATAJDBC-95 @Test // DATAJDBC-95
public void saveAndLoadAnEntity() { public void saveAndLoadAnEntity() {
@ -249,12 +273,22 @@ public class JdbcRepositoryIntegrationTests {
return entity; return entity;
} }
interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> {} private static DummyEntity createExistingDummyEntity() {
DummyEntity entity = new DummyEntity();
entity.setIdProp(Long.parseLong("123"));
entity.setName("Entity Name");
return entity;
}
interface DummyEntityRepository extends CrudRepository<DummyEntity, Long>, JdbcRepository<DummyEntity, Long> {
}
@Data @Data
static class DummyEntity { static class DummyEntity {
String name; String name;
@Id private Long idProp; @Id
private Long idProp;
} }
} }

274
spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/AbstractRelationalEntityWriter.java

@ -0,0 +1,274 @@
package org.springframework.data.relational.core.conversion;
import lombok.Value;
import org.springframework.data.convert.EntityWriter;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.mapping.PersistentPropertyPaths;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.data.util.Pair;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Used as an abstract class to derive relational writer actions (save, insert, update).
* Implementations see {@link RelationalEntityWriter} and {@link RelationalEntityInsertWriter}
*
* @author Thomas Lang
*/
abstract class AbstractRelationalEntityWriter implements EntityWriter<Object, AggregateChange<?>> {
protected final RelationalMappingContext context;
AbstractRelationalEntityWriter(RelationalMappingContext context) {
this.context = context;
}
/**
* Holds context information for the current save operation.
*/
class WritingContext {
private final Object root;
private final Object entity;
private final Class<?> entityType;
private final PersistentPropertyPaths<?, RelationalPersistentProperty> paths;
private final Map<PathNode, DbAction> previousActions = new HashMap<>();
private Map<PersistentPropertyPath<RelationalPersistentProperty>, List<PathNode>> nodesCache = new HashMap<>();
WritingContext(Object root, AggregateChange<?> aggregateChange) {
this.root = root;
this.entity = aggregateChange.getEntity();
this.entityType = aggregateChange.getEntityType();
this.paths = context.findPersistentPropertyPaths(entityType, PersistentProperty::isEntity);
}
/**
* Leaves out the isNew check as defined in #DATAJDBC-282
*
* @return List of {@link DbAction}s
* @see <a href="https://jira.spring.io/browse/DATAJDBC-282">DAJDBC-282</a>
*/
List<DbAction<?>> insert() {
List<DbAction<?>> actions = new ArrayList<>();
actions.add(setRootAction(new DbAction.InsertRoot<>(entity)));
actions.addAll(insertReferenced());
return actions;
}
/**
* Leaves out the isNew check as defined in #DATAJDBC-282
*
* @return List of {@link DbAction}s
* @see <a href="https://jira.spring.io/browse/DATAJDBC-282">DAJDBC-282</a>
*/
List<DbAction<?>> update() {
List<DbAction<?>> actions = new ArrayList<>(deleteReferenced());
actions.add(setRootAction(new DbAction.UpdateRoot<>(entity)));
actions.addAll(insertReferenced());
return actions;
}
List<DbAction<?>> save() {
List<DbAction<?>> actions = new ArrayList<>();
if (isNew(root)) {
actions.add(setRootAction(new DbAction.InsertRoot<>(entity)));
actions.addAll(insertReferenced());
} else {
actions.addAll(deleteReferenced());
actions.add(setRootAction(new DbAction.UpdateRoot<>(entity)));
actions.addAll(insertReferenced());
}
return actions;
}
private boolean isNew(Object o) {
return context.getRequiredPersistentEntity(o.getClass()).isNew(o);
}
//// Operations on all paths
private List<DbAction<?>> insertReferenced() {
List<DbAction<?>> actions = new ArrayList<>();
paths.forEach(path -> actions.addAll(insertAll(path)));
return actions;
}
private List<DbAction<?>> insertAll(PersistentPropertyPath<RelationalPersistentProperty> path) {
List<DbAction<?>> actions = new ArrayList<>();
from(path).forEach(node -> {
DbAction.Insert<Object> insert;
if (node.path.getRequiredLeafProperty().isQualified()) {
Pair<Object, Object> value = (Pair) node.getValue();
insert = new DbAction.Insert<>(value.getSecond(), path, getAction(node.parent));
insert.getAdditionalValues().put(node.path.getRequiredLeafProperty().getKeyColumn(), value.getFirst());
} else {
insert = new DbAction.Insert<>(node.getValue(), path, getAction(node.parent));
}
previousActions.put(node, insert);
actions.add(insert);
});
return actions;
}
private List<DbAction<?>> deleteReferenced() {
List<DbAction<?>> deletes = new ArrayList<>();
paths.forEach(path -> deletes.add(0, deleteReferenced(path)));
return deletes;
}
/// Operations on a single path
private DbAction.Delete<?> deleteReferenced(PersistentPropertyPath<RelationalPersistentProperty> path) {
Object id = context.getRequiredPersistentEntity(entityType).getIdentifierAccessor(entity).getIdentifier();
return new DbAction.Delete<>(id, path);
}
//// methods not directly related to the creation of DbActions
private DbAction<?> setRootAction(DbAction<?> dbAction) {
previousActions.put(null, dbAction);
return dbAction;
}
@Nullable
private DbAction.WithEntity<?> getAction(@Nullable RelationalEntityInsertWriter.PathNode parent) {
DbAction action = previousActions.get(parent);
if (action != null) {
Assert.isInstanceOf( //
DbAction.WithEntity.class, //
action, //
"dependsOn action is not a WithEntity, but " + action.getClass().getSimpleName() //
);
return (DbAction.WithEntity<?>) action;
}
return null;
}
// commented as of #DATAJDBC-282
// private boolean isNew(Object o) {
// return context.getRequiredPersistentEntity(o.getClass()).isNew(o);
// }
private List<RelationalEntityInsertWriter.PathNode> from(
PersistentPropertyPath<RelationalPersistentProperty> path) {
List<RelationalEntityInsertWriter.PathNode> nodes = new ArrayList<>();
if (path.getLength() == 1) {
Object value = context //
.getRequiredPersistentEntity(entityType) //
.getPropertyAccessor(entity) //
.getProperty(path.getRequiredLeafProperty());
nodes.addAll(createNodes(path, null, value));
} else {
List<RelationalEntityInsertWriter.PathNode> pathNodes = nodesCache.get(path.getParentPath());
pathNodes.forEach(parentNode -> {
Object value = path.getRequiredLeafProperty().getOwner().getPropertyAccessor(parentNode.getValue())
.getProperty(path.getRequiredLeafProperty());
nodes.addAll(createNodes(path, parentNode, value));
});
}
nodesCache.put(path, nodes);
return nodes;
}
private List<RelationalEntityInsertWriter.PathNode> createNodes(
PersistentPropertyPath<RelationalPersistentProperty> path,
@Nullable RelationalEntityInsertWriter.PathNode parentNode, @Nullable Object value) {
if (value == null) {
return Collections.emptyList();
}
List<RelationalEntityInsertWriter.PathNode> nodes = new ArrayList<>();
if (path.getRequiredLeafProperty().isQualified()) {
if (path.getRequiredLeafProperty().isMap()) {
((Map<?, ?>) value)
.forEach((k, v) -> nodes.add(new RelationalEntityInsertWriter.PathNode(path, parentNode, Pair.of(k, v))));
} else {
List listValue = (List) value;
for (int k = 0; k < listValue.size(); k++) {
nodes.add(new RelationalEntityInsertWriter.PathNode(path, parentNode, Pair.of(k, listValue.get(k))));
}
}
} else if (path.getRequiredLeafProperty().isCollectionLike()) { // collection value
((Collection<?>) value).forEach(v -> nodes.add(new RelationalEntityInsertWriter.PathNode(path, parentNode, v)));
} else { // single entity value
nodes.add(new RelationalEntityInsertWriter.PathNode(path, parentNode, value));
}
return nodes;
}
}
/**
* Represents a single entity in an aggregate along with its property path from the root entity and the chain of
* objects to traverse a long this path.
*/
@Value
static class PathNode {
/**
* The path to this entity
*/
PersistentPropertyPath<RelationalPersistentProperty> path;
/**
* The parent {@link PathNode}. This is {@code null} if this is the root entity.
*/
@Nullable
PathNode parent;
/**
* The value of the entity.
*/
Object value;
}
}

44
spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/RelationalEntityInsertWriter.java

@ -0,0 +1,44 @@
/*
* 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.relational.core.conversion;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import java.util.List;
/**
* Converts an aggregate represented by its root into an {@link AggregateChange}.
* Does not perform any isNew check.
*
* @author Jens Schauder
* @author Mark Paluch
* @author Thomas Lang
*/
public class RelationalEntityInsertWriter extends AbstractRelationalEntityWriter {
public RelationalEntityInsertWriter(RelationalMappingContext context) {
super(context);
}
/*
* (non-Javadoc)
* @see org.springframework.data.convert.EntityWriter#save(java.lang.Object, java.lang.Object)
*/
@Override public void write(Object root, AggregateChange<?> aggregateChange) {
List<DbAction<?>> actions = new WritingContext(root, aggregateChange).insert();
actions.forEach(aggregateChange::addAction);
}
}

44
spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/RelationalEntityUpdateWriter.java

@ -0,0 +1,44 @@
/*
* 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.relational.core.conversion;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import java.util.List;
/**
* Converts an aggregate represented by its root into an {@link AggregateChange}.
* Does not perform any isNew check.
*
* @author Jens Schauder
* @author Mark Paluch
* @author Thomas Lang
*/
public class RelationalEntityUpdateWriter extends AbstractRelationalEntityWriter {
public RelationalEntityUpdateWriter(RelationalMappingContext context) {
super(context);
}
/*
* (non-Javadoc)
* @see org.springframework.data.convert.EntityWriter#save(java.lang.Object, java.lang.Object)
*/
@Override public void write(Object root, AggregateChange<?> aggregateChange) {
List<DbAction<?>> actions = new WritingContext(root, aggregateChange).update();
actions.forEach(aggregateChange::addAction);
}
}

232
spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/RelationalEntityWriter.java

@ -15,24 +15,9 @@
*/ */
package org.springframework.data.relational.core.conversion; package org.springframework.data.relational.core.conversion;
import lombok.Value; import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import org.springframework.data.convert.EntityWriter;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.mapping.PersistentPropertyPaths;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.data.util.Pair;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/** /**
* Converts an aggregate represented by its root into an {@link AggregateChange}. * Converts an aggregate represented by its root into an {@link AggregateChange}.
@ -40,223 +25,18 @@ import org.springframework.util.Assert;
* @author Jens Schauder * @author Jens Schauder
* @author Mark Paluch * @author Mark Paluch
*/ */
public class RelationalEntityWriter implements EntityWriter<Object, AggregateChange<?>> { public class RelationalEntityWriter extends AbstractRelationalEntityWriter {
private final RelationalMappingContext context;
public RelationalEntityWriter(RelationalMappingContext context) { public RelationalEntityWriter(RelationalMappingContext context) {
this.context = context; super(context);
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.convert.EntityWriter#write(java.lang.Object, java.lang.Object) * @see org.springframework.data.convert.EntityWriter#save(java.lang.Object, java.lang.Object)
*/ */
@Override @Override public void write(Object root, AggregateChange<?> aggregateChange) {
public void write(Object root, AggregateChange<?> aggregateChange) { List<DbAction<?>> actions = new WritingContext(root, aggregateChange).save();
List<DbAction<?>> actions = new WritingContext(root, aggregateChange).write();
actions.forEach(aggregateChange::addAction); actions.forEach(aggregateChange::addAction);
} }
/**
* Holds context information for the current write operation.
*/
private class WritingContext {
private final Object root;
private final Object entity;
private final Class<?> entityType;
private final PersistentPropertyPaths<?, RelationalPersistentProperty> paths;
private final Map<PathNode, DbAction> previousActions = new HashMap<>();
private Map<PersistentPropertyPath<RelationalPersistentProperty>, List<PathNode>> nodesCache = new HashMap<>();
WritingContext(Object root, AggregateChange<?> aggregateChange) {
this.root = root;
this.entity = aggregateChange.getEntity();
this.entityType = aggregateChange.getEntityType();
this.paths = context.findPersistentPropertyPaths(entityType, PersistentProperty::isEntity);
}
private List<DbAction<?>> write() {
List<DbAction<?>> actions = new ArrayList<>();
if (isNew(root)) {
actions.add(setRootAction(new DbAction.InsertRoot<>(entity)));
actions.addAll(insertReferenced());
} else {
actions.addAll(deleteReferenced());
actions.add(setRootAction(new DbAction.UpdateRoot<>(entity)));
actions.addAll(insertReferenced());
}
return actions;
}
//// Operations on all paths
private List<DbAction<?>> insertReferenced() {
List<DbAction<?>> actions = new ArrayList<>();
paths.forEach(path -> actions.addAll(insertAll(path)));
return actions;
}
private List<DbAction<?>> insertAll(PersistentPropertyPath<RelationalPersistentProperty> path) {
List<DbAction<?>> actions = new ArrayList<>();
from(path).forEach(node -> {
DbAction.Insert<Object> insert;
if (node.path.getRequiredLeafProperty().isQualified()) {
Pair<Object, Object> value = (Pair) node.getValue();
insert = new DbAction.Insert<>(value.getSecond(), path, getAction(node.parent));
insert.getAdditionalValues().put(node.path.getRequiredLeafProperty().getKeyColumn(), value.getFirst());
} else {
insert = new DbAction.Insert<>(node.getValue(), path, getAction(node.parent));
}
previousActions.put(node, insert);
actions.add(insert);
});
return actions;
}
private List<DbAction<?>> deleteReferenced() {
List<DbAction<?>> deletes = new ArrayList<>();
paths.forEach(path -> deletes.add(0, deleteReferenced(path)));
return deletes;
}
/// Operations on a single path
private DbAction.Delete<?> deleteReferenced(PersistentPropertyPath<RelationalPersistentProperty> path) {
Object id = context.getRequiredPersistentEntity(entityType).getIdentifierAccessor(entity).getIdentifier();
return new DbAction.Delete<>(id, path);
}
//// methods not directly related to the creation of DbActions
private DbAction<?> setRootAction(DbAction<?> dbAction) {
previousActions.put(null, dbAction);
return dbAction;
}
@Nullable
private DbAction.WithEntity<?> getAction(@Nullable PathNode parent) {
DbAction action = previousActions.get(parent);
if (action != null) {
Assert.isInstanceOf( //
DbAction.WithEntity.class, //
action, //
"dependsOn action is not a WithEntity, but " + action.getClass().getSimpleName() //
);
return (DbAction.WithEntity<?>) action;
}
return null;
}
private boolean isNew(Object o) {
return context.getRequiredPersistentEntity(o.getClass()).isNew(o);
}
private List<PathNode> from(PersistentPropertyPath<RelationalPersistentProperty> path) {
List<PathNode> nodes = new ArrayList<>();
if (path.getLength() == 1) {
Object value = context //
.getRequiredPersistentEntity(entityType) //
.getPropertyAccessor(entity) //
.getProperty(path.getRequiredLeafProperty());
nodes.addAll(createNodes(path, null, value));
} else {
List<PathNode> pathNodes = nodesCache.get(path.getParentPath());
pathNodes.forEach(parentNode -> {
Object value = path.getRequiredLeafProperty().getOwner().getPropertyAccessor(parentNode.getValue())
.getProperty(path.getRequiredLeafProperty());
nodes.addAll(createNodes(path, parentNode, value));
});
}
nodesCache.put(path, nodes);
return nodes;
}
private List<PathNode> createNodes(PersistentPropertyPath<RelationalPersistentProperty> path,
@Nullable PathNode parentNode, @Nullable Object value) {
if (value == null) {
return Collections.emptyList();
}
List<PathNode> nodes = new ArrayList<>();
if (path.getRequiredLeafProperty().isQualified()) {
if (path.getRequiredLeafProperty().isMap()) {
((Map<?, ?>) value).forEach((k, v) -> nodes.add(new PathNode(path, parentNode, Pair.of(k, v))));
} else {
List listValue = (List) value;
for (int k = 0; k < listValue.size(); k++) {
nodes.add(new PathNode(path, parentNode, Pair.of(k, listValue.get(k))));
}
}
} else if (path.getRequiredLeafProperty().isCollectionLike()) { // collection value
((Collection<?>) value).forEach(v -> nodes.add(new PathNode(path, parentNode, v)));
} else { // single entity value
nodes.add(new PathNode(path, parentNode, value));
}
return nodes;
}
}
/**
* Represents a single entity in an aggregate along with its property path from the root entity and the chain of
* objects to traverse a long this path.
*/
@Value
static class PathNode {
/** The path to this entity */
PersistentPropertyPath<RelationalPersistentProperty> path;
/**
* The parent {@link PathNode}. This is {@code null} if this is the root entity.
*/
@Nullable PathNode parent;
/** The value of the entity. */
Object value;
}
} }

111
spring-data-relational/src/test/java/org/springframework/data/relational/core/conversion/RelationalEntityInsertWriterUnitTests.java

@ -0,0 +1,111 @@
/*
* 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.relational.core.conversion;
import lombok.RequiredArgsConstructor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.conversion.AggregateChange.Kind;
import org.springframework.data.relational.core.conversion.DbAction.InsertRoot;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import static org.assertj.core.api.Assertions.*;
/**
* Unit tests for the {@link RelationalEntityInsertWriter}
*
* @author Jens Schauder
* @author Thomas Lang
*/
@RunWith(MockitoJUnitRunner.class) public class RelationalEntityInsertWriterUnitTests {
public static final long SOME_ENTITY_ID = 23L;
RelationalEntityInsertWriter converter = new RelationalEntityInsertWriter(new RelationalMappingContext());
@Test // DATAJDBC-112
public void newEntityGetsConvertedToOneInsert() {
SingleReferenceEntity entity = new SingleReferenceEntity(null);
AggregateChange<SingleReferenceEntity> aggregateChange = //
new AggregateChange(Kind.SAVE, SingleReferenceEntity.class, entity);
converter.write(entity, aggregateChange);
assertThat(aggregateChange.getActions()) //
.extracting(DbAction::getClass, DbAction::getEntityType, this::extractPath, this::actualEntityType,
this::isWithDependsOn) //
.containsExactly( //
tuple(InsertRoot.class, SingleReferenceEntity.class, "", SingleReferenceEntity.class, false) //
);
}
@Test // DATAJDBC-282
public void existingEntityGetsNotConvertedToDeletePlusUpdate() {
SingleReferenceEntity entity = new SingleReferenceEntity(SOME_ENTITY_ID);
AggregateChange<SingleReferenceEntity> aggregateChange = //
new AggregateChange(Kind.SAVE, SingleReferenceEntity.class, entity);
converter.write(entity, aggregateChange);
assertThat(aggregateChange.getActions()) //
.extracting(DbAction::getClass, DbAction::getEntityType, this::extractPath, this::actualEntityType,
this::isWithDependsOn) //
.containsExactly( //
tuple(InsertRoot.class, SingleReferenceEntity.class, "", SingleReferenceEntity.class, false) //
);
assertThat(aggregateChange.getEntity()).isNotNull();
// the new id should not be the same as the origin one - should do insert, not update
// assertThat(aggregateChange.getEntity().id).isNotEqualTo(SOME_ENTITY_ID);
}
private String extractPath(DbAction action) {
if (action instanceof DbAction.WithPropertyPath) {
return ((DbAction.WithPropertyPath<?>) action).getPropertyPath().toDotPath();
}
return "";
}
private boolean isWithDependsOn(DbAction dbAction) {
return dbAction instanceof DbAction.WithDependingOn;
}
private Class<?> actualEntityType(DbAction a) {
if (a instanceof DbAction.WithEntity) {
return ((DbAction.WithEntity) a).getEntity().getClass();
}
return null;
}
@RequiredArgsConstructor static class SingleReferenceEntity {
@Id final Long id;
Element other;
// should not trigger own Dbaction
String name;
}
@RequiredArgsConstructor private static class Element {
@Id final Long id;
}
}

96
spring-data-relational/src/test/java/org/springframework/data/relational/core/conversion/RelationalEntityUpdateWriterUnitTests.java

@ -0,0 +1,96 @@
/*
* 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.relational.core.conversion;
import lombok.RequiredArgsConstructor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.conversion.AggregateChange.Kind;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import static org.assertj.core.api.Assertions.*;
/**
* Unit tests for the {@link RelationalEntityUpdateWriter}
*
* @author Jens Schauder
* @author Thomas Lang
*/
@RunWith(MockitoJUnitRunner.class)
public class RelationalEntityUpdateWriterUnitTests {
public static final long SOME_ENTITY_ID = 23L;
RelationalEntityUpdateWriter converter = new RelationalEntityUpdateWriter(new RelationalMappingContext());
@Test // DATAJDBC-112
public void existingEntityGetsConvertedToDeletePlusUpdate() {
SingleReferenceEntity entity = new SingleReferenceEntity(SOME_ENTITY_ID);
AggregateChange<RelationalEntityWriterUnitTests.SingleReferenceEntity> aggregateChange = //
new AggregateChange(Kind.SAVE, SingleReferenceEntity.class, entity);
converter.write(entity, aggregateChange);
assertThat(aggregateChange.getActions()) //
.extracting(DbAction::getClass, DbAction::getEntityType, this::extractPath, this::actualEntityType,
this::isWithDependsOn) //
.containsExactly( //
tuple(DbAction.Delete.class, Element.class, "other", null, false), //
tuple(DbAction.UpdateRoot.class, SingleReferenceEntity.class, "", SingleReferenceEntity.class, false) //
);
}
private String extractPath(DbAction action) {
if (action instanceof DbAction.WithPropertyPath) {
return ((DbAction.WithPropertyPath<?>) action).getPropertyPath().toDotPath();
}
return "";
}
private boolean isWithDependsOn(DbAction dbAction) {
return dbAction instanceof DbAction.WithDependingOn;
}
private Class<?> actualEntityType(DbAction a) {
if (a instanceof DbAction.WithEntity) {
return ((DbAction.WithEntity) a).getEntity().getClass();
}
return null;
}
@RequiredArgsConstructor
static class SingleReferenceEntity {
@Id
final Long id;
Element other;
// should not trigger own Dbaction
String name;
}
@RequiredArgsConstructor
private static class Element {
@Id
final Long id;
}
}
Loading…
Cancel
Save