From 65d4b38428d4ff77d0a2440bc7d1e3c0447a9d82 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Thu, 15 Nov 2018 10:31:53 +0100 Subject: [PATCH] DATAJDBC-294 - Fixed handling of Id names in WHERE- clauses. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit RelationalPersistentEntityImpl.getIdColumn wasn’t respecting @Column annotations. --- .../AggregateTemplateIntegrationTests.java | 4 ++++ .../data/jdbc/core/SqlGeneratorUnitTests.java | 20 +++++++++++-------- ...AggregateTemplateIntegrationTests-hsql.sql | 10 +++++----- ...regateTemplateIntegrationTests-mariadb.sql | 10 +++++----- ...ggregateTemplateIntegrationTests-mssql.sql | 10 +++++----- ...ggregateTemplateIntegrationTests-mysql.sql | 10 +++++----- ...egateTemplateIntegrationTests-postgres.sql | 10 +++++----- .../RelationalPersistentEntityImpl.java | 2 +- ...lationalPersistentEntityImplUnitTests.java | 13 +++++++++++- 9 files changed, 54 insertions(+), 35 deletions(-) diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/AggregateTemplateIntegrationTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/AggregateTemplateIntegrationTests.java index 095f1915..2bca5dca 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/AggregateTemplateIntegrationTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/AggregateTemplateIntegrationTests.java @@ -333,6 +333,7 @@ public class AggregateTemplateIntegrationTests { @Data static class LegoSet { + @Column("id1") @Id private Long id; private String name; @@ -344,6 +345,7 @@ public class AggregateTemplateIntegrationTests { @Data static class Manual { + @Column("id2") @Id private Long id; private String content; @@ -351,6 +353,7 @@ public class AggregateTemplateIntegrationTests { static class OneToOneParent { + @Column("id3") @Id private Long id; private String content; @@ -363,6 +366,7 @@ public class AggregateTemplateIntegrationTests { static class ListParent { + @Column("id4") @Id private Long id; String name; List content = new ArrayList<>(); diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/SqlGeneratorUnitTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/SqlGeneratorUnitTests.java index 6a2d5464..da9929e9 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/SqlGeneratorUnitTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/SqlGeneratorUnitTests.java @@ -29,6 +29,7 @@ import org.springframework.data.jdbc.core.mapping.AggregateReference; import org.springframework.data.jdbc.core.mapping.JdbcMappingContext; import org.springframework.data.jdbc.core.mapping.PersistentPropertyPathTestUtils; import org.springframework.data.mapping.PersistentPropertyPath; +import org.springframework.data.relational.core.mapping.Column; import org.springframework.data.relational.core.mapping.NamingStrategy; import org.springframework.data.relational.core.mapping.RelationalMappingContext; import org.springframework.data.relational.core.mapping.RelationalPersistentEntity; @@ -68,11 +69,13 @@ public class SqlGeneratorUnitTests { SoftAssertions softAssertions = new SoftAssertions(); softAssertions.assertThat(sql) // .startsWith("SELECT") // - .contains("dummy_entity.x_id AS x_id,") // + .contains("dummy_entity.id1 AS id1,") // .contains("dummy_entity.x_name AS x_name,") // .contains("dummy_entity.x_other AS x_other,") // .contains("ref.x_l1id AS ref_x_l1id") // .contains("ref.x_content AS ref_x_content").contains(" FROM dummy_entity") // + .contains("ON ref.dummy_entity = dummy_entity.id1") // + .contains("WHERE dummy_entity.id1 = :id") // // 1-N relationships do not get loaded via join .doesNotContain("Element AS elements"); softAssertions.assertAll(); @@ -142,10 +145,10 @@ public class SqlGeneratorUnitTests { // 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, " // + assertThat(sql).isEqualTo("SELECT dummy_entity.id1 AS id1, dummy_entity.x_name AS x_name, " // + "dummy_entity.x_other AS x_other, " // + "ref.x_l1id AS ref_x_l1id, ref.x_content AS ref_x_content, ref.x_further AS ref_x_further " // - + "FROM dummy_entity LEFT OUTER JOIN referenced_entity AS ref ON ref.dummy_entity = dummy_entity.x_id " // + + "FROM dummy_entity LEFT OUTER JOIN referenced_entity AS ref ON ref.dummy_entity = dummy_entity.id1 " // + "WHERE back-ref = :back-ref"); } @@ -155,11 +158,11 @@ public class SqlGeneratorUnitTests { // 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, " // + assertThat(sql).isEqualTo("SELECT dummy_entity.id1 AS id1, dummy_entity.x_name AS x_name, " // + "dummy_entity.x_other AS x_other, " // + "ref.x_l1id AS ref_x_l1id, ref.x_content AS ref_x_content, ref.x_further AS ref_x_further, " // + "dummy_entity.key-column AS key-column " // - + "FROM dummy_entity LEFT OUTER JOIN referenced_entity AS ref ON ref.dummy_entity = dummy_entity.x_id " // + + "FROM dummy_entity LEFT OUTER JOIN referenced_entity AS ref ON ref.dummy_entity = dummy_entity.id1 " // + "WHERE back-ref = :back-ref"); } @@ -174,11 +177,11 @@ public class SqlGeneratorUnitTests { // 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, " // + assertThat(sql).isEqualTo("SELECT dummy_entity.id1 AS id1, dummy_entity.x_name AS x_name, " // + "dummy_entity.x_other AS x_other, " // + "ref.x_l1id AS ref_x_l1id, ref.x_content AS ref_x_content, ref.x_further AS ref_x_further, " // + "dummy_entity.key-column AS key-column " // - + "FROM dummy_entity LEFT OUTER JOIN referenced_entity AS ref ON ref.dummy_entity = dummy_entity.x_id " // + + "FROM dummy_entity LEFT OUTER JOIN referenced_entity AS ref ON ref.dummy_entity = dummy_entity.id1 " // + "WHERE back-ref = :back-ref " + "ORDER BY key-column"); } @@ -211,7 +214,7 @@ public class SqlGeneratorUnitTests { "dummy_entity", // "SET", // "WHERE", // - "x_id = :x_id"); + "id1 = :id"); } private PersistentPropertyPath getPath(String path, Class base) { @@ -221,6 +224,7 @@ public class SqlGeneratorUnitTests { @SuppressWarnings("unused") static class DummyEntity { + @Column("id1") @Id Long id; String name; ReferencedEntity ref; diff --git a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-hsql.sql b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-hsql.sql index 81f69885..6354676d 100644 --- a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-hsql.sql +++ b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-hsql.sql @@ -1,11 +1,11 @@ -CREATE TABLE LEGO_SET ( id BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY, NAME VARCHAR(30)); -CREATE TABLE MANUAL ( id BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY, LEGO_SET BIGINT, ALTERNATIVE BIGINT, CONTENT VARCHAR(2000)); +CREATE TABLE LEGO_SET ( id1 BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY, NAME VARCHAR(30)); +CREATE TABLE MANUAL ( id2 BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY, LEGO_SET BIGINT, ALTERNATIVE BIGINT, CONTENT VARCHAR(2000)); ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET) -REFERENCES LEGO_SET(id); +REFERENCES LEGO_SET(id1); -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_PARENT ( id3 BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) 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 LIST_PARENT ( id4 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/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-mariadb.sql b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-mariadb.sql index 51b77c98..07ecf86b 100644 --- a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-mariadb.sql +++ b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-mariadb.sql @@ -1,11 +1,11 @@ -CREATE TABLE LEGO_SET ( id BIGINT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(30)); -CREATE TABLE MANUAL ( id BIGINT AUTO_INCREMENT PRIMARY KEY, LEGO_SET BIGINT, ALTERNATIVE BIGINT, CONTENT VARCHAR(2000)); +CREATE TABLE LEGO_SET ( id1 BIGINT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(30)); +CREATE TABLE MANUAL ( id2 BIGINT AUTO_INCREMENT PRIMARY KEY, LEGO_SET BIGINT, ALTERNATIVE BIGINT, CONTENT VARCHAR(2000)); ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET) -REFERENCES LEGO_SET(id); +REFERENCES LEGO_SET(id1); -CREATE TABLE ONE_TO_ONE_PARENT ( id BIGINT AUTO_INCREMENT PRIMARY KEY, content VARCHAR(30)); +CREATE TABLE ONE_TO_ONE_PARENT ( id3 BIGINT AUTO_INCREMENT 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 LIST_PARENT ( id4 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/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-mssql.sql b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-mssql.sql index 12d81c9a..bae84431 100644 --- a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-mssql.sql +++ b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-mssql.sql @@ -1,15 +1,15 @@ DROP TABLE IF EXISTS LEGO_SET; DROP TABLE IF EXISTS MANUAL; -CREATE TABLE LEGO_SET ( id BIGINT IDENTITY PRIMARY KEY, NAME VARCHAR(30)); -CREATE TABLE MANUAL ( id BIGINT IDENTITY PRIMARY KEY, LEGO_SET BIGINT, ALTERNATIVE BIGINT, CONTENT VARCHAR(2000)); -ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET) REFERENCES LEGO_SET(id); +CREATE TABLE LEGO_SET ( id1 BIGINT IDENTITY PRIMARY KEY, NAME VARCHAR(30)); +CREATE TABLE MANUAL ( id2 BIGINT IDENTITY PRIMARY KEY, LEGO_SET BIGINT, ALTERNATIVE BIGINT, CONTENT VARCHAR(2000)); +ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET) REFERENCES LEGO_SET(id1); DROP TABLE IF EXISTS ONE_TO_ONE_PARENT; DROP TABLE IF EXISTS Child_No_Id; -CREATE TABLE ONE_TO_ONE_PARENT ( id BIGINT IDENTITY PRIMARY KEY, content VARCHAR(30)); +CREATE TABLE ONE_TO_ONE_PARENT ( id3 BIGINT IDENTITY PRIMARY KEY, content VARCHAR(30)); CREATE TABLE Child_No_Id (ONE_TO_ONE_PARENT BIGINT PRIMARY KEY, content VARCHAR(30)); DROP TABLE IF EXISTS LIST_PARENT; DROP TABLE IF EXISTS element_no_id; -CREATE TABLE LIST_PARENT ( id BIGINT IDENTITY PRIMARY KEY, NAME VARCHAR(100)); +CREATE TABLE LIST_PARENT ( id4 BIGINT IDENTITY PRIMARY KEY, NAME VARCHAR(100)); CREATE TABLE element_no_id ( content VARCHAR(100), LIST_PARENT_key BIGINT, LIST_PARENT BIGINT); diff --git a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-mysql.sql b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-mysql.sql index 51b77c98..07ecf86b 100644 --- a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-mysql.sql +++ b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-mysql.sql @@ -1,11 +1,11 @@ -CREATE TABLE LEGO_SET ( id BIGINT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(30)); -CREATE TABLE MANUAL ( id BIGINT AUTO_INCREMENT PRIMARY KEY, LEGO_SET BIGINT, ALTERNATIVE BIGINT, CONTENT VARCHAR(2000)); +CREATE TABLE LEGO_SET ( id1 BIGINT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(30)); +CREATE TABLE MANUAL ( id2 BIGINT AUTO_INCREMENT PRIMARY KEY, LEGO_SET BIGINT, ALTERNATIVE BIGINT, CONTENT VARCHAR(2000)); ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET) -REFERENCES LEGO_SET(id); +REFERENCES LEGO_SET(id1); -CREATE TABLE ONE_TO_ONE_PARENT ( id BIGINT AUTO_INCREMENT PRIMARY KEY, content VARCHAR(30)); +CREATE TABLE ONE_TO_ONE_PARENT ( id3 BIGINT AUTO_INCREMENT 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 LIST_PARENT ( id4 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/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-postgres.sql b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-postgres.sql index 63b4aba7..57511192 100644 --- a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-postgres.sql +++ b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-postgres.sql @@ -1,14 +1,14 @@ DROP TABLE MANUAL; DROP TABLE LEGO_SET; -CREATE TABLE LEGO_SET ( id SERIAL PRIMARY KEY, NAME VARCHAR(30)); -CREATE TABLE MANUAL ( id SERIAL PRIMARY KEY, LEGO_SET BIGINT, ALTERNATIVE BIGINT, CONTENT VARCHAR(2000)); +CREATE TABLE LEGO_SET ( id1 SERIAL PRIMARY KEY, NAME VARCHAR(30)); +CREATE TABLE MANUAL ( id2 SERIAL PRIMARY KEY, LEGO_SET BIGINT, ALTERNATIVE BIGINT, CONTENT VARCHAR(2000)); ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET) -REFERENCES LEGO_SET(id); +REFERENCES LEGO_SET(id1); -CREATE TABLE ONE_TO_ONE_PARENT ( id SERIAL PRIMARY KEY, content VARCHAR(30)); +CREATE TABLE ONE_TO_ONE_PARENT ( id3 SERIAL 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 LIST_PARENT ( id4 SERIAL PRIMARY KEY, NAME VARCHAR(100)); CREATE TABLE element_no_id ( content VARCHAR(100), LIST_PARENT_key BIGINT, LIST_PARENT INTEGER); diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/RelationalPersistentEntityImpl.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/RelationalPersistentEntityImpl.java index 464a87e8..6b916a11 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/RelationalPersistentEntityImpl.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/RelationalPersistentEntityImpl.java @@ -62,7 +62,7 @@ class RelationalPersistentEntityImpl extends BasicPersistentEntity entity = mappingContext.getPersistentEntity(DummySubEntity.class); + + assertThat(entity.getIdColumn()).isEqualTo("renamedId"); + } + @Table("dummy_sub_entity") - static class DummySubEntity {} + static class DummySubEntity { + @Id @Column("renamedId") Long id; + } }