DATAJDBC-294 - Fixed handling of Id names in WHERE-clauses.

RelationalPersistentEntityImpl.getIdColumn wasn’t respecting @Column annotations.
This commit is contained in:
Jens Schauder
2018-11-15 10:31:53 +01:00
parent 67007354db
commit 054c32c7f4
8 changed files with 39 additions and 23 deletions

View File

@@ -62,7 +62,7 @@ class RelationalPersistentEntityImpl<T> extends BasicPersistentEntity<T, Relatio
*/
@Override
public String getIdColumn() {
return this.namingStrategy.getColumnName(getRequiredIdProperty());
return getRequiredIdProperty().getColumnName();
}
/*

View File

@@ -32,6 +32,7 @@ import org.springframework.context.annotation.Import;
import org.springframework.data.annotation.Id;
import org.springframework.data.jdbc.testing.TestConfiguration;
import org.springframework.data.relational.core.conversion.RelationalConverter;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.rules.SpringClassRule;
@@ -225,7 +226,7 @@ public class AggregateTemplateIntegrationTests {
@Data
static class LegoSet {
@Id private Long id;
@Column("id1") @Id private Long id;
private String name;
@@ -236,7 +237,7 @@ public class AggregateTemplateIntegrationTests {
@Data
static class Manual {
@Id private Long id;
@Column("id2") @Id private Long id;
private String content;
}

View File

@@ -27,6 +27,7 @@ import org.junit.Test;
import org.springframework.data.annotation.Id;
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;
@@ -66,10 +67,12 @@ 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("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();
@@ -139,9 +142,9 @@ public class SqlGeneratorUnitTests {
// this would get called when DummyEntity 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, "
+ "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");
}
@@ -151,10 +154,10 @@ public class SqlGeneratorUnitTests {
// this would get called when DummyEntity 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, " //
+ "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");
}
@@ -169,10 +172,10 @@ public class SqlGeneratorUnitTests {
// this would get called when DummyEntity 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, "
+ "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");
}
@@ -193,6 +196,7 @@ public class SqlGeneratorUnitTests {
@SuppressWarnings("unused")
static class DummyEntity {
@Column("id1")
@Id Long id;
String name;
ReferencedEntity ref;

View File

@@ -22,6 +22,7 @@ import org.springframework.data.relational.core.mapping.RelationalMappingContext
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntityImpl;
import org.springframework.data.relational.core.mapping.Table;
import org.springframework.data.annotation.Id;
/**
* Unit tests for {@link RelationalPersistentEntityImpl}.
@@ -41,6 +42,16 @@ public class RelationalPersistentEntityImplUnitTests {
assertThat(entity.getTableName()).isEqualTo("dummy_sub_entity");
}
@Test // DATAJDBC-294
public void considerIdColumnName() {
RelationalPersistentEntity<?> 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;
}
}

View File

@@ -1,5 +1,5 @@
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, 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, CONTENT VARCHAR(2000));
ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET)
REFERENCES LEGO_SET(id);
REFERENCES LEGO_SET(id1);

View File

@@ -1,5 +1,5 @@
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, 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, CONTENT VARCHAR(2000));
ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET)
REFERENCES LEGO_SET(id);
REFERENCES LEGO_SET(id1);

View File

@@ -1,5 +1,5 @@
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, 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, CONTENT VARCHAR(2000));
ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET)
REFERENCES LEGO_SET(id);
REFERENCES LEGO_SET(id1);

View File

@@ -1,8 +1,8 @@
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, CONTENT VARCHAR(2000));
CREATE TABLE LEGO_SET ( id1 SERIAL PRIMARY KEY, NAME VARCHAR(30));
CREATE TABLE MANUAL ( id2 SERIAL PRIMARY KEY, LEGO_SET BIGINT, CONTENT VARCHAR(2000));
ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET)
REFERENCES LEGO_SET(id);
REFERENCES LEGO_SET(id1);