DATAJDBC-276 - Id are no longer required for elements of Lists.
This commit is contained in:
committed by
Greg Turnquist
parent
9fb5aadd89
commit
c47cd0878d
@@ -39,6 +39,9 @@ import org.springframework.test.context.junit4.rules.SpringClassRule;
|
||||
import org.springframework.test.context.junit4.rules.SpringMethodRule;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link JdbcAggregateTemplate}.
|
||||
*
|
||||
@@ -217,7 +220,7 @@ public class AggregateTemplateIntegrationTests {
|
||||
OneToOneParent parent = new OneToOneParent();
|
||||
|
||||
parent.content = "parent content";
|
||||
parent.child = new OneToOneChildNoId();
|
||||
parent.child = new ChildNoId();
|
||||
parent.child.content = "child content";
|
||||
|
||||
template.save(parent);
|
||||
@@ -248,7 +251,7 @@ public class AggregateTemplateIntegrationTests {
|
||||
OneToOneParent parent = new OneToOneParent();
|
||||
|
||||
parent.content = "parent content";
|
||||
parent.child = new OneToOneChildNoId();
|
||||
parent.child = new ChildNoId();
|
||||
|
||||
template.save(parent);
|
||||
|
||||
@@ -289,6 +292,23 @@ public class AggregateTemplateIntegrationTests {
|
||||
|
||||
softly.assertAll();
|
||||
}
|
||||
@Test // DATAJDBC-276
|
||||
public void saveAndLoadAnEntityWithListOfElementsWithoutId() {
|
||||
|
||||
ListParent entity = new ListParent();
|
||||
entity.name = "name";
|
||||
|
||||
ElementNoId element = new ElementNoId();
|
||||
element.content = "content";
|
||||
|
||||
entity.content.add(element);
|
||||
|
||||
template.save(entity);
|
||||
|
||||
ListParent reloaded = template.findById(entity.id, ListParent.class);
|
||||
|
||||
assertThat(reloaded.content).extracting(e -> e.content).containsExactly("content");
|
||||
}
|
||||
|
||||
private static LegoSet createLegoSet() {
|
||||
|
||||
@@ -326,13 +346,25 @@ public class AggregateTemplateIntegrationTests {
|
||||
@Id private Long id;
|
||||
private String content;
|
||||
|
||||
private OneToOneChildNoId child;
|
||||
private ChildNoId child;
|
||||
}
|
||||
|
||||
static class OneToOneChildNoId {
|
||||
static class ChildNoId {
|
||||
private String content;
|
||||
}
|
||||
|
||||
static class ListParent {
|
||||
|
||||
@Id private Long id;
|
||||
String name;
|
||||
List<ElementNoId> content = new ArrayList<>();
|
||||
}
|
||||
|
||||
static class ElementNoId {
|
||||
private String content;
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
@Import(TestConfiguration.class)
|
||||
static class Config {
|
||||
|
||||
@@ -139,7 +139,7 @@ public class SqlGeneratorUnitTests {
|
||||
@Test // DATAJDBC-131
|
||||
public void findAllByProperty() {
|
||||
|
||||
// this would get called when DummyEntity is the element type of a Set
|
||||
// this would get called when ListParent is the element type of a Set
|
||||
String sql = sqlGenerator.getFindAllByProperty("back-ref", null, false);
|
||||
|
||||
assertThat(sql).isEqualTo("SELECT dummy_entity.x_id AS x_id, dummy_entity.x_name AS x_name, " //
|
||||
@@ -152,7 +152,7 @@ public class SqlGeneratorUnitTests {
|
||||
@Test // DATAJDBC-131
|
||||
public void findAllByPropertyWithKey() {
|
||||
|
||||
// this would get called when DummyEntity is th element type of a Map
|
||||
// this would get called when ListParent is th element type of a Map
|
||||
String sql = sqlGenerator.getFindAllByProperty("back-ref", "key-column", false);
|
||||
|
||||
assertThat(sql).isEqualTo("SELECT dummy_entity.x_id AS x_id, dummy_entity.x_name AS x_name, " //
|
||||
@@ -171,7 +171,7 @@ public class SqlGeneratorUnitTests {
|
||||
@Test // DATAJDBC-131
|
||||
public void findAllByPropertyWithKeyOrdered() {
|
||||
|
||||
// this would get called when DummyEntity is th element type of a Map
|
||||
// this would get called when ListParent is th element type of a Map
|
||||
String sql = sqlGenerator.getFindAllByProperty("back-ref", "key-column", true);
|
||||
|
||||
assertThat(sql).isEqualTo("SELECT dummy_entity.x_id AS x_id, dummy_entity.x_name AS x_name, " //
|
||||
|
||||
@@ -5,4 +5,7 @@ ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET)
|
||||
REFERENCES LEGO_SET(id);
|
||||
|
||||
CREATE TABLE ONE_TO_ONE_PARENT ( id BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY, content VARCHAR(30));
|
||||
CREATE TABLE One_To_One_Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));
|
||||
CREATE TABLE Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));
|
||||
|
||||
CREATE TABLE LIST_PARENT ( id BIGINT GENERATED BY DEFAULT AS IDENTITY ( START WITH 1 ) PRIMARY KEY, NAME VARCHAR(100));
|
||||
CREATE TABLE element_no_id ( content VARCHAR(100), LIST_PARENT_key BIGINT, LIST_PARENT BIGINT);
|
||||
|
||||
@@ -5,4 +5,7 @@ ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET)
|
||||
REFERENCES LEGO_SET(id);
|
||||
|
||||
CREATE TABLE ONE_TO_ONE_PARENT ( id BIGINT AUTO_INCREMENT PRIMARY KEY, content VARCHAR(30));
|
||||
CREATE TABLE One_To_One_Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));
|
||||
CREATE TABLE Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));
|
||||
|
||||
CREATE TABLE LIST_PARENT ( id BIGINT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(100));
|
||||
CREATE TABLE element_no_id ( content VARCHAR(100), LIST_PARENT_key BIGINT, LIST_PARENT BIGINT);
|
||||
|
||||
@@ -5,4 +5,7 @@ ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET)
|
||||
REFERENCES LEGO_SET(id);
|
||||
|
||||
CREATE TABLE ONE_TO_ONE_PARENT ( id BIGINT AUTO_INCREMENT PRIMARY KEY, content VARCHAR(30));
|
||||
CREATE TABLE One_To_One_Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));
|
||||
CREATE TABLE Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));
|
||||
|
||||
CREATE TABLE LIST_PARENT ( id BIGINT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(100));
|
||||
CREATE TABLE element_no_id ( content VARCHAR(100), LIST_PARENT_key BIGINT, LIST_PARENT BIGINT);
|
||||
|
||||
@@ -8,4 +8,7 @@ ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET)
|
||||
REFERENCES LEGO_SET(id);
|
||||
|
||||
CREATE TABLE ONE_TO_ONE_PARENT ( id SERIAL PRIMARY KEY, content VARCHAR(30));
|
||||
CREATE TABLE One_To_One_Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));
|
||||
CREATE TABLE Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));
|
||||
|
||||
CREATE TABLE LIST_PARENT ( id SERIAL PRIMARY KEY, NAME VARCHAR(100));
|
||||
CREATE TABLE element_no_id ( content VARCHAR(100), LIST_PARENT_key BIGINT, LIST_PARENT INTEGER);
|
||||
|
||||
@@ -107,14 +107,6 @@ public class AggregateChange<T> {
|
||||
|
||||
PersistentPropertyPath<RelationalPersistentProperty> propertyPathToEntity = action.getPropertyPath();
|
||||
|
||||
RelationalPersistentProperty requiredIdProperty = context
|
||||
.getRequiredPersistentEntity(propertyPathToEntity.getRequiredLeafProperty().getActualType())
|
||||
.getRequiredIdProperty();
|
||||
|
||||
PersistentPropertyPath<RelationalPersistentProperty> pathToId = context.getPersistentPropertyPath(
|
||||
propertyPathToEntity.toDotPath() + '.' + requiredIdProperty.getName(),
|
||||
propertyPathToEntity.getBaseProperty().getOwner().getType());
|
||||
|
||||
RelationalPersistentProperty leafProperty = propertyPathToEntity.getRequiredLeafProperty();
|
||||
|
||||
Object currentPropertyValue = propertyAccessor.getProperty(propertyPathToEntity);
|
||||
@@ -140,6 +132,15 @@ public class AggregateChange<T> {
|
||||
throw new IllegalStateException("Can't handle " + currentPropertyValue);
|
||||
}
|
||||
} else {
|
||||
|
||||
RelationalPersistentProperty requiredIdProperty = context
|
||||
.getRequiredPersistentEntity(propertyPathToEntity.getRequiredLeafProperty().getActualType())
|
||||
.getRequiredIdProperty();
|
||||
|
||||
PersistentPropertyPath<RelationalPersistentProperty> pathToId = context.getPersistentPropertyPath(
|
||||
propertyPathToEntity.toDotPath() + '.' + requiredIdProperty.getName(),
|
||||
propertyPathToEntity.getBaseProperty().getOwner().getType());
|
||||
|
||||
propertyAccessor.setProperty(pathToId, generatedId);
|
||||
}
|
||||
}
|
||||
@@ -191,7 +192,11 @@ public class AggregateChange<T> {
|
||||
PersistentPropertyAccessor<T> intermediateAccessor = converter.getPropertyAccessor(persistentEntity,
|
||||
(T) originalElement);
|
||||
|
||||
intermediateAccessor.setProperty(persistentEntity.getRequiredIdProperty(), generatedId);
|
||||
RelationalPersistentProperty idProperty = persistentEntity.getIdProperty();
|
||||
if (idProperty != null) {
|
||||
intermediateAccessor.setProperty(idProperty, generatedId);
|
||||
}
|
||||
|
||||
return intermediateAccessor;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user