diff --git a/pom.xml b/pom.xml
index 7fc8f36b75..f46dfe6b14 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
org.springframework.data
spring-data-jdbc
- 1.0.0.BUILD-SNAPSHOT
+ 1.0.0.DATAJDBC-145-SNAPSHOT
Spring Data JDBC
Spring Data module for JDBC repositories.
@@ -15,14 +15,14 @@
org.springframework.data.build
spring-data-parent
- 2.0.0.BUILD-SNAPSHOT
+ 2.1.0.BUILD-SNAPSHOT
DATAJDBC
- 2.0.0.BUILD-SNAPSHOT
+ 2.1.0.BUILD-SNAPSHOT
spring.data.jdbc
reuseReports
diff --git a/src/main/java/org/springframework/data/jdbc/core/DefaultJdbcInterpreter.java b/src/main/java/org/springframework/data/jdbc/core/DefaultJdbcInterpreter.java
index 3109078899..e2ce916c0e 100644
--- a/src/main/java/org/springframework/data/jdbc/core/DefaultJdbcInterpreter.java
+++ b/src/main/java/org/springframework/data/jdbc/core/DefaultJdbcInterpreter.java
@@ -26,6 +26,8 @@
import org.springframework.data.jdbc.core.conversion.Interpreter;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity;
+import org.springframework.data.mapping.PropertyPath;
+import org.springframework.util.Assert;
/**
* {@link Interpreter} for {@link DbAction}s using a {@link DataAccessStrategy} for performing actual database
@@ -60,15 +62,15 @@ public void interpret(Delete delete) {
if (delete.getPropertyPath() == null) {
accessStrategy.delete(delete.getRootId(), delete.getEntityType());
} else {
- accessStrategy.delete(delete.getRootId(), delete.getPropertyPath());
+ accessStrategy.delete(delete.getRootId(), delete.getPropertyPath().getPath());
}
}
@Override
public void interpret(DeleteAll delete) {
-
+
if (delete.getEntityType() == null) {
- accessStrategy.deleteAll(delete.getPropertyPath());
+ accessStrategy.deleteAll(delete.getPropertyPath().getPath());
} else {
accessStrategy.deleteAll(delete.getEntityType());
}
@@ -84,16 +86,32 @@ private Map createAdditionalColumnValues(Insert insert) {
}
private void addDependingOnInformation(Insert insert, Map additionalColumnValues) {
+
DbAction dependingOn = insert.getDependingOn();
- if (dependingOn != null) {
+ if (dependingOn == null) {
+ return;
+ }
- JdbcPersistentEntity> persistentEntity = context.getRequiredPersistentEntity(dependingOn.getEntityType());
- String columnName = persistentEntity.getTableName();
- Object entity = dependingOn.getEntity();
- Object identifier = persistentEntity.getIdentifierAccessor(entity).getIdentifier();
+ JdbcPersistentEntity> persistentEntity = context.getRequiredPersistentEntity(dependingOn.getEntityType());
- additionalColumnValues.put(columnName, identifier);
- }
+ String columnName = getColumnNameForReverseColumn(insert, persistentEntity);
+
+ Object identifier = getIdFromEntityDependingOn(dependingOn, persistentEntity);
+
+ additionalColumnValues.put(columnName, identifier);
+ }
+
+ private Object getIdFromEntityDependingOn(DbAction dependingOn, JdbcPersistentEntity> persistentEntity) {
+ return persistentEntity.getIdentifierAccessor(dependingOn.getEntity()).getIdentifier();
+ }
+
+ private String getColumnNameForReverseColumn(Insert insert, JdbcPersistentEntity> persistentEntity) {
+
+ PropertyPath path = insert.getPropertyPath().getPath();
+
+ Assert.notNull(path, "There shouldn't be an insert depending on another insert without having a PropertyPath.");
+
+ return persistentEntity.getRequiredPersistentProperty(path.getSegment()).getReverseColumnName();
}
}
diff --git a/src/main/java/org/springframework/data/jdbc/core/SqlGenerator.java b/src/main/java/org/springframework/data/jdbc/core/SqlGenerator.java
index 37ef48d422..502d6d0f3b 100644
--- a/src/main/java/org/springframework/data/jdbc/core/SqlGenerator.java
+++ b/src/main/java/org/springframework/data/jdbc/core/SqlGenerator.java
@@ -26,7 +26,6 @@
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty;
-import org.springframework.data.jdbc.mapping.model.PropertyPaths;
import org.springframework.data.jdbc.repository.SimpleJdbcRepository;
import org.springframework.data.mapping.PropertyHandler;
import org.springframework.data.mapping.PropertyPath;
@@ -267,7 +266,7 @@ String createDeleteAllSql(PropertyPath path) {
return String.format("DELETE FROM %s", entity.getTableName());
}
- JdbcPersistentEntity> entityToDelete = context.getRequiredPersistentEntity(PropertyPaths.getLeafType(path));
+ JdbcPersistentEntity> entityToDelete = context.getRequiredPersistentEntity(path.getLeafType());
JdbcPersistentEntity> owningEntity = context.getRequiredPersistentEntity(path.getOwningType());
JdbcPersistentProperty property = owningEntity.getRequiredPersistentProperty(path.getSegment());
@@ -285,7 +284,7 @@ private String createDeleteByListSql() {
String createDeleteByPath(PropertyPath path) {
- JdbcPersistentEntity> entityToDelete = context.getRequiredPersistentEntity(PropertyPaths.getLeafType(path));
+ JdbcPersistentEntity> entityToDelete = context.getRequiredPersistentEntity(path.getLeafType());
JdbcPersistentEntity> owningEntity = context.getRequiredPersistentEntity(path.getOwningType());
JdbcPersistentProperty property = owningEntity.getRequiredPersistentProperty(path.getSegment());
diff --git a/src/main/java/org/springframework/data/jdbc/core/conversion/DbAction.java b/src/main/java/org/springframework/data/jdbc/core/conversion/DbAction.java
index f25d031ed7..e5f2fee59d 100644
--- a/src/main/java/org/springframework/data/jdbc/core/conversion/DbAction.java
+++ b/src/main/java/org/springframework/data/jdbc/core/conversion/DbAction.java
@@ -21,7 +21,6 @@
import java.util.HashMap;
import java.util.Map;
-import org.springframework.data.mapping.PropertyPath;
import org.springframework.util.Assert;
/**
@@ -43,6 +42,11 @@ public abstract class DbAction {
*/
private final T entity;
+ /**
+ * The path from the Aggregate Root to the entity affected by this {@link DbAction}.
+ */
+ private final JdbcPropertyPath propertyPath;
+
/**
* Key-value-pairs to specify additional values to be used with the statement which can't be obtained from the entity,
* nor from {@link DbAction}s {@literal this} depends on. A used case are map keys, which need to be persisted with
@@ -57,27 +61,28 @@ public abstract class DbAction {
*/
private final DbAction dependingOn;
- private DbAction(Class entityType, T entity, DbAction dependingOn) {
+ private DbAction(Class entityType, T entity, JdbcPropertyPath propertyPath, DbAction dependingOn) {
this.entityType = entityType;
this.entity = entity;
+ this.propertyPath = propertyPath;
this.dependingOn = dependingOn;
}
- public static Insert insert(T entity, DbAction dependingOn) {
- return new Insert<>(entity, dependingOn);
+ public static Insert insert(T entity, JdbcPropertyPath propertyPath, DbAction dependingOn) {
+ return new Insert<>(entity, propertyPath, dependingOn);
}
- public static Update update(T entity, DbAction dependingOn) {
- return new Update<>(entity, dependingOn);
+ public static Update update(T entity, JdbcPropertyPath propertyPath, DbAction dependingOn) {
+ return new Update<>(entity, propertyPath, dependingOn);
}
- public static Delete delete(Object id, Class type, T entity, PropertyPath propertyPath,
+ public static Delete delete(Object id, Class type, T entity, JdbcPropertyPath propertyPath,
DbAction dependingOn) {
return new Delete<>(id, type, entity, propertyPath, dependingOn);
}
- public static DeleteAll deleteAll(Class type, PropertyPath propertyPath, DbAction dependingOn) {
+ public static DeleteAll deleteAll(Class type, JdbcPropertyPath propertyPath, DbAction dependingOn) {
return new DeleteAll<>(type, propertyPath, dependingOn);
}
@@ -90,8 +95,9 @@ public static DeleteAll deleteAll(Class type, PropertyPath propertyPat
*/
abstract static class InsertOrUpdate extends DbAction {
- InsertOrUpdate(T entity, DbAction dependingOn) {
- super((Class) entity.getClass(), entity, dependingOn);
+ @SuppressWarnings("unchecked")
+ InsertOrUpdate(T entity, JdbcPropertyPath propertyPath, DbAction dependingOn) {
+ super((Class) entity.getClass(), entity, propertyPath, dependingOn);
}
}
@@ -102,8 +108,8 @@ abstract static class InsertOrUpdate extends DbAction {
*/
public static class Insert extends InsertOrUpdate {
- private Insert(T entity, DbAction dependingOn) {
- super(entity, dependingOn);
+ private Insert(T entity, JdbcPropertyPath propertyPath, DbAction dependingOn) {
+ super(entity, propertyPath, dependingOn);
}
@Override
@@ -119,8 +125,8 @@ void executeWith(Interpreter interpreter) {
*/
public static class Update extends InsertOrUpdate {
- private Update(T entity, DbAction dependingOn) {
- super(entity, dependingOn);
+ private Update(T entity, JdbcPropertyPath propertyPath, DbAction dependingOn) {
+ super(entity, propertyPath, dependingOn);
}
@Override
@@ -143,20 +149,13 @@ public static class Delete extends DbAction {
*/
private final Object rootId;
- /**
- * {@link PropertyPath} which connects the aggregate root with the entities to be deleted. If this is the action to
- * delete the root enity itself, this is {@literal null}.
- */
- private final PropertyPath propertyPath;
-
- private Delete(Object rootId, Class type, T entity, PropertyPath propertyPath, DbAction dependingOn) {
+ private Delete(Object rootId, Class type, T entity, JdbcPropertyPath propertyPath, DbAction dependingOn) {
- super(type, entity, dependingOn);
+ super(type, entity, propertyPath, dependingOn);
Assert.notNull(rootId, "rootId must not be null.");
this.rootId = rootId;
- this.propertyPath = propertyPath;
}
@Override
@@ -170,19 +169,10 @@ void executeWith(Interpreter interpreter) {
*
* @param type o the entity for which this represents a database interaction
*/
- @Getter
public static class DeleteAll extends DbAction {
- /**
- *
- */
- private final PropertyPath propertyPath;
-
- private DeleteAll(Class entityType, PropertyPath propertyPath, DbAction dependingOn) {
-
- super(entityType, null, dependingOn);
-
- this.propertyPath = propertyPath;
+ private DeleteAll(Class entityType, JdbcPropertyPath propertyPath, DbAction dependingOn) {
+ super(entityType, null, propertyPath, dependingOn);
}
@Override
diff --git a/src/main/java/org/springframework/data/jdbc/core/conversion/JdbcEntityDeleteWriter.java b/src/main/java/org/springframework/data/jdbc/core/conversion/JdbcEntityDeleteWriter.java
index 429c75a914..7aa6cfd40e 100644
--- a/src/main/java/org/springframework/data/jdbc/core/conversion/JdbcEntityDeleteWriter.java
+++ b/src/main/java/org/springframework/data/jdbc/core/conversion/JdbcEntityDeleteWriter.java
@@ -16,7 +16,6 @@
package org.springframework.data.jdbc.core.conversion;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
-import org.springframework.data.jdbc.mapping.model.PropertyPaths;
/**
* Converts an entity that is about to be deleted into {@link DbAction}s inside a {@link AggregateChange} that need to be
@@ -43,7 +42,7 @@ public void write(Object id, AggregateChange aggregateChange) {
private void deleteAll(AggregateChange aggregateChange) {
context.referencedEntities(aggregateChange.getEntityType(), null)
- .forEach(p -> aggregateChange.addAction(DbAction.deleteAll(PropertyPaths.getLeafType(p), p, null)));
+ .forEach(p -> aggregateChange.addAction(DbAction.deleteAll(p.getLeafType(), new JdbcPropertyPath(p), null)));
aggregateChange.addAction(DbAction.deleteAll(aggregateChange.getEntityType(), null, null));
}
@@ -54,5 +53,4 @@ private void deleteById(Object id, AggregateChange aggregateChange) {
aggregateChange.addAction(DbAction.delete(id, aggregateChange.getEntityType(), aggregateChange.getEntity(), null, null));
}
-
}
diff --git a/src/main/java/org/springframework/data/jdbc/core/conversion/JdbcEntityWriter.java b/src/main/java/org/springframework/data/jdbc/core/conversion/JdbcEntityWriter.java
index 478ce68b33..c04f152984 100644
--- a/src/main/java/org/springframework/data/jdbc/core/conversion/JdbcEntityWriter.java
+++ b/src/main/java/org/springframework/data/jdbc/core/conversion/JdbcEntityWriter.java
@@ -52,76 +52,85 @@ public void write(Object o, AggregateChange aggregateChange) {
private void write(Object o, AggregateChange aggregateChange, DbAction dependingOn) {
- JdbcPersistentEntityInformation