From 680e237b23778da23bf64347cfd9dbdd882ab6bc Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Mon, 15 Oct 2018 10:45:58 +0200 Subject: [PATCH 1/2] DATAJDBC-276 - Prepare branch. --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index deedf29b73..4a22deeeda 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.springframework.data spring-data-jdbc - 1.1.0.BUILD-SNAPSHOT + 1.1.0.DATAJDBC-276-SNAPSHOT Spring Data JDBC Spring Data module for JDBC repositories. From 308b3da37d6c38ff11805242c95bfd4c58b2b355 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Mon, 15 Oct 2018 11:39:51 +0200 Subject: [PATCH 2/2] DATAJDBC-276 - Id are no longer required for elements of Lists. --- .../core/conversion/AggregateChange.java | 23 ++++++----- .../AggregateTemplateIntegrationTests.java | 40 +++++++++++++++++-- .../data/jdbc/core/SqlGeneratorUnitTests.java | 6 +-- ...AggregateTemplateIntegrationTests-hsql.sql | 5 ++- ...regateTemplateIntegrationTests-mariadb.sql | 5 ++- ...ggregateTemplateIntegrationTests-mysql.sql | 5 ++- ...egateTemplateIntegrationTests-postgres.sql | 5 ++- 7 files changed, 69 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/springframework/data/relational/core/conversion/AggregateChange.java b/src/main/java/org/springframework/data/relational/core/conversion/AggregateChange.java index ed14500fa7..2a0044201e 100644 --- a/src/main/java/org/springframework/data/relational/core/conversion/AggregateChange.java +++ b/src/main/java/org/springframework/data/relational/core/conversion/AggregateChange.java @@ -107,14 +107,6 @@ static void setId(RelationalMappingContext context, RelationalConverter converte PersistentPropertyPath propertyPathToEntity = action.getPropertyPath(); - RelationalPersistentProperty requiredIdProperty = context - .getRequiredPersistentEntity(propertyPathToEntity.getRequiredLeafProperty().getActualType()) - .getRequiredIdProperty(); - - PersistentPropertyPath pathToId = context.getPersistentPropertyPath( - propertyPathToEntity.toDotPath() + '.' + requiredIdProperty.getName(), - propertyPathToEntity.getBaseProperty().getOwner().getType()); - RelationalPersistentProperty leafProperty = propertyPathToEntity.getRequiredLeafProperty(); Object currentPropertyValue = propertyAccessor.getProperty(propertyPathToEntity); @@ -140,6 +132,15 @@ static void setId(RelationalMappingContext context, RelationalConverter converte throw new IllegalStateException("Can't handle " + currentPropertyValue); } } else { + + RelationalPersistentProperty requiredIdProperty = context + .getRequiredPersistentEntity(propertyPathToEntity.getRequiredLeafProperty().getActualType()) + .getRequiredIdProperty(); + + PersistentPropertyPath pathToId = context.getPersistentPropertyPath( + propertyPathToEntity.toDotPath() + '.' + requiredIdProperty.getName(), + propertyPathToEntity.getBaseProperty().getOwner().getType()); + propertyAccessor.setProperty(pathToId, generatedId); } } @@ -191,7 +192,11 @@ private static PersistentPropertyAccessor setId(RelationalConverter conve PersistentPropertyAccessor intermediateAccessor = converter.getPropertyAccessor(persistentEntity, (T) originalElement); - intermediateAccessor.setProperty(persistentEntity.getRequiredIdProperty(), generatedId); + RelationalPersistentProperty idProperty = persistentEntity.getIdProperty(); + if (idProperty != null) { + intermediateAccessor.setProperty(idProperty, generatedId); + } + return intermediateAccessor; } diff --git a/src/test/java/org/springframework/data/jdbc/core/AggregateTemplateIntegrationTests.java b/src/test/java/org/springframework/data/jdbc/core/AggregateTemplateIntegrationTests.java index 7c2196b720..bdb5436613 100644 --- a/src/test/java/org/springframework/data/jdbc/core/AggregateTemplateIntegrationTests.java +++ b/src/test/java/org/springframework/data/jdbc/core/AggregateTemplateIntegrationTests.java @@ -39,6 +39,9 @@ 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 void oneToOneChildWithoutId() { 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 void oneToOneNullAttributes() { OneToOneParent parent = new OneToOneParent(); parent.content = "parent content"; - parent.child = new OneToOneChildNoId(); + parent.child = new ChildNoId(); template.save(parent); @@ -289,6 +292,23 @@ public void saveAndLoadAnEntityWithSecondaryReferenceNotNull() { 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() { @@ -327,13 +347,25 @@ static class OneToOneParent { @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 content = new ArrayList<>(); + } + + static class ElementNoId { + private String content; + } + + @Configuration @Import(TestConfiguration.class) static class Config { diff --git a/src/test/java/org/springframework/data/jdbc/core/SqlGeneratorUnitTests.java b/src/test/java/org/springframework/data/jdbc/core/SqlGeneratorUnitTests.java index 9578d6c7e7..7c6725f8a7 100644 --- a/src/test/java/org/springframework/data/jdbc/core/SqlGeneratorUnitTests.java +++ b/src/test/java/org/springframework/data/jdbc/core/SqlGeneratorUnitTests.java @@ -138,7 +138,7 @@ public void deleteMapByPath() { @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, " // @@ -151,7 +151,7 @@ public void findAllByProperty() { @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, " // @@ -170,7 +170,7 @@ public void findAllByPropertyOrderedWithoutKey() { @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, " // diff --git a/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-hsql.sql b/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-hsql.sql index 35d1682dd8..81f698852f 100644 --- a/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-hsql.sql +++ b/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-hsql.sql @@ -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); diff --git a/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-mariadb.sql b/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-mariadb.sql index 3bc7423620..51b77c9851 100644 --- a/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-mariadb.sql +++ b/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-mariadb.sql @@ -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); diff --git a/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-mysql.sql b/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-mysql.sql index 3bc7423620..51b77c9851 100644 --- a/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-mysql.sql +++ b/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-mysql.sql @@ -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); diff --git a/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-postgres.sql b/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-postgres.sql index 8a7223d4f6..63b4aba71a 100644 --- a/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-postgres.sql +++ b/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-postgres.sql @@ -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);