Skip to content

Commit 054c32c

Browse files
committed
DATAJDBC-294 - Fixed handling of Id names in WHERE-clauses.
RelationalPersistentEntityImpl.getIdColumn wasn’t respecting @column annotations.
1 parent 6700735 commit 054c32c

File tree

8 files changed

+39
-23
lines changed

8 files changed

+39
-23
lines changed

src/main/java/org/springframework/data/relational/core/mapping/RelationalPersistentEntityImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public String getTableName() {
6262
*/
6363
@Override
6464
public String getIdColumn() {
65-
return this.namingStrategy.getColumnName(getRequiredIdProperty());
65+
return getRequiredIdProperty().getColumnName();
6666
}
6767

6868
/*

src/test/java/org/springframework/data/jdbc/core/AggregateTemplateIntegrationTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.data.annotation.Id;
3333
import org.springframework.data.jdbc.testing.TestConfiguration;
3434
import org.springframework.data.relational.core.conversion.RelationalConverter;
35+
import org.springframework.data.relational.core.mapping.Column;
3536
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
3637
import org.springframework.test.context.ContextConfiguration;
3738
import org.springframework.test.context.junit4.rules.SpringClassRule;
@@ -225,7 +226,7 @@ private static LegoSet createLegoSet() {
225226
@Data
226227
static class LegoSet {
227228

228-
@Id private Long id;
229+
@Column("id1") @Id private Long id;
229230

230231
private String name;
231232

@@ -236,7 +237,7 @@ static class LegoSet {
236237
@Data
237238
static class Manual {
238239

239-
@Id private Long id;
240+
@Column("id2") @Id private Long id;
240241
private String content;
241242

242243
}

src/test/java/org/springframework/data/jdbc/core/SqlGeneratorUnitTests.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.data.annotation.Id;
2828
import org.springframework.data.jdbc.core.mapping.PersistentPropertyPathTestUtils;
2929
import org.springframework.data.mapping.PersistentPropertyPath;
30+
import org.springframework.data.relational.core.mapping.Column;
3031
import org.springframework.data.relational.core.mapping.NamingStrategy;
3132
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
3233
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
@@ -66,10 +67,12 @@ public void findOne() {
6667
SoftAssertions softAssertions = new SoftAssertions();
6768
softAssertions.assertThat(sql) //
6869
.startsWith("SELECT") //
69-
.contains("dummy_entity.x_id AS x_id,") //
70+
.contains("dummy_entity.id1 AS id1,") //
7071
.contains("dummy_entity.x_name AS x_name,") //
7172
.contains("ref.x_l1id AS ref_x_l1id") //
7273
.contains("ref.x_content AS ref_x_content").contains(" FROM dummy_entity") //
74+
.contains("ON ref.dummy_entity = dummy_entity.id1") //
75+
.contains("WHERE dummy_entity.id1 = :id") //
7376
// 1-N relationships do not get loaded via join
7477
.doesNotContain("Element AS elements");
7578
softAssertions.assertAll();
@@ -139,9 +142,9 @@ public void findAllByProperty() {
139142
// this would get called when DummyEntity is the element type of a Set
140143
String sql = sqlGenerator.getFindAllByProperty("back-ref", null, false);
141144

142-
assertThat(sql).isEqualTo("SELECT dummy_entity.x_id AS x_id, dummy_entity.x_name AS x_name, "
145+
assertThat(sql).isEqualTo("SELECT dummy_entity.id1 AS id1, dummy_entity.x_name AS x_name, "
143146
+ "ref.x_l1id AS ref_x_l1id, ref.x_content AS ref_x_content, ref.x_further AS ref_x_further "
144-
+ "FROM dummy_entity LEFT OUTER JOIN referenced_entity AS ref ON ref.dummy_entity = dummy_entity.x_id "
147+
+ "FROM dummy_entity LEFT OUTER JOIN referenced_entity AS ref ON ref.dummy_entity = dummy_entity.id1 "
145148
+ "WHERE back-ref = :back-ref");
146149
}
147150

@@ -151,10 +154,10 @@ public void findAllByPropertyWithKey() {
151154
// this would get called when DummyEntity is th element type of a Map
152155
String sql = sqlGenerator.getFindAllByProperty("back-ref", "key-column", false);
153156

154-
assertThat(sql).isEqualTo("SELECT dummy_entity.x_id AS x_id, dummy_entity.x_name AS x_name, " //
157+
assertThat(sql).isEqualTo("SELECT dummy_entity.id1 AS id1, dummy_entity.x_name AS x_name, " //
155158
+ "ref.x_l1id AS ref_x_l1id, ref.x_content AS ref_x_content, ref.x_further AS ref_x_further, " //
156159
+ "dummy_entity.key-column AS key-column " //
157-
+ "FROM dummy_entity LEFT OUTER JOIN referenced_entity AS ref ON ref.dummy_entity = dummy_entity.x_id " //
160+
+ "FROM dummy_entity LEFT OUTER JOIN referenced_entity AS ref ON ref.dummy_entity = dummy_entity.id1 " //
158161
+ "WHERE back-ref = :back-ref");
159162
}
160163

@@ -169,10 +172,10 @@ public void findAllByPropertyWithKeyOrdered() {
169172
// this would get called when DummyEntity is th element type of a Map
170173
String sql = sqlGenerator.getFindAllByProperty("back-ref", "key-column", true);
171174

172-
assertThat(sql).isEqualTo("SELECT dummy_entity.x_id AS x_id, dummy_entity.x_name AS x_name, "
175+
assertThat(sql).isEqualTo("SELECT dummy_entity.id1 AS id1, dummy_entity.x_name AS x_name, "
173176
+ "ref.x_l1id AS ref_x_l1id, ref.x_content AS ref_x_content, ref.x_further AS ref_x_further, "
174177
+ "dummy_entity.key-column AS key-column "
175-
+ "FROM dummy_entity LEFT OUTER JOIN referenced_entity AS ref ON ref.dummy_entity = dummy_entity.x_id "
178+
+ "FROM dummy_entity LEFT OUTER JOIN referenced_entity AS ref ON ref.dummy_entity = dummy_entity.id1 "
176179
+ "WHERE back-ref = :back-ref " + "ORDER BY key-column");
177180
}
178181

@@ -193,6 +196,7 @@ private PersistentPropertyPath<RelationalPersistentProperty> getPath(String path
193196
@SuppressWarnings("unused")
194197
static class DummyEntity {
195198

199+
@Column("id1")
196200
@Id Long id;
197201
String name;
198202
ReferencedEntity ref;

src/test/java/org/springframework/data/relational/core/mapping/RelationalPersistentEntityImplUnitTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
2323
import org.springframework.data.relational.core.mapping.RelationalPersistentEntityImpl;
2424
import org.springframework.data.relational.core.mapping.Table;
25+
import org.springframework.data.annotation.Id;
2526

2627
/**
2728
* Unit tests for {@link RelationalPersistentEntityImpl}.
@@ -41,6 +42,16 @@ public void discoversAnnotatedTableName() {
4142
assertThat(entity.getTableName()).isEqualTo("dummy_sub_entity");
4243
}
4344

45+
@Test // DATAJDBC-294
46+
public void considerIdColumnName() {
47+
48+
RelationalPersistentEntity<?> entity = mappingContext.getPersistentEntity(DummySubEntity.class);
49+
50+
assertThat(entity.getIdColumn()).isEqualTo("renamedId");
51+
}
52+
4453
@Table("dummy_sub_entity")
45-
static class DummySubEntity {}
54+
static class DummySubEntity {
55+
@Id @Column("renamedId") Long id;
56+
}
4657
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
CREATE TABLE LEGO_SET ( id BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY, NAME VARCHAR(30));
2-
CREATE TABLE MANUAL ( id BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY, LEGO_SET BIGINT, CONTENT VARCHAR(2000));
1+
CREATE TABLE LEGO_SET ( id1 BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY, NAME VARCHAR(30));
2+
CREATE TABLE MANUAL ( id2 BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY, LEGO_SET BIGINT, CONTENT VARCHAR(2000));
33

44
ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET)
5-
REFERENCES LEGO_SET(id);
5+
REFERENCES LEGO_SET(id1);
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
CREATE TABLE LEGO_SET ( id BIGINT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(30));
2-
CREATE TABLE MANUAL ( id BIGINT AUTO_INCREMENT PRIMARY KEY, LEGO_SET BIGINT, CONTENT VARCHAR(2000));
1+
CREATE TABLE LEGO_SET ( id1 BIGINT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(30));
2+
CREATE TABLE MANUAL ( id2 BIGINT AUTO_INCREMENT PRIMARY KEY, LEGO_SET BIGINT, CONTENT VARCHAR(2000));
33

44
ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET)
5-
REFERENCES LEGO_SET(id);
5+
REFERENCES LEGO_SET(id1);
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
CREATE TABLE LEGO_SET ( id BIGINT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(30));
2-
CREATE TABLE MANUAL ( id BIGINT AUTO_INCREMENT PRIMARY KEY, LEGO_SET BIGINT, CONTENT VARCHAR(2000));
1+
CREATE TABLE LEGO_SET ( id1 BIGINT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(30));
2+
CREATE TABLE MANUAL ( id2 BIGINT AUTO_INCREMENT PRIMARY KEY, LEGO_SET BIGINT, CONTENT VARCHAR(2000));
33

44
ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET)
5-
REFERENCES LEGO_SET(id);
5+
REFERENCES LEGO_SET(id1);
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
DROP TABLE MANUAL;
22
DROP TABLE LEGO_SET;
33

4-
CREATE TABLE LEGO_SET ( id SERIAL PRIMARY KEY, NAME VARCHAR(30));
5-
CREATE TABLE MANUAL ( id SERIAL PRIMARY KEY, LEGO_SET BIGINT, CONTENT VARCHAR(2000));
4+
CREATE TABLE LEGO_SET ( id1 SERIAL PRIMARY KEY, NAME VARCHAR(30));
5+
CREATE TABLE MANUAL ( id2 SERIAL PRIMARY KEY, LEGO_SET BIGINT, CONTENT VARCHAR(2000));
66

77
ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET)
8-
REFERENCES LEGO_SET(id);
8+
REFERENCES LEGO_SET(id1);

0 commit comments

Comments
 (0)