Skip to content

Commit 482f330

Browse files
schaudergregturn
authored andcommitted
DATAJDBC-144 - Using correct column names from NamingStrategy.
The list of columns used in the SqlGenerator contained property names instead of column names, leading to errors when a non-trivial NamingStrategy was used.
1 parent 3706826 commit 482f330

File tree

4 files changed

+37
-19
lines changed

4 files changed

+37
-19
lines changed

src/main/java/org/springframework/data/jdbc/core/SqlGenerator.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ class SqlGenerator {
4343

4444
private final JdbcPersistentEntity<?> entity;
4545
private final JdbcMappingContext context;
46-
private final List<String> propertyNames = new ArrayList<>();
47-
private final List<String> nonIdPropertyNames = new ArrayList<>();
46+
private final List<String> columnNames = new ArrayList<>();
47+
private final List<String> nonIdColumnNames = new ArrayList<>();
4848

4949
private final Lazy<String> findOneSql = Lazy.of(this::createFindOneSelectSql);
5050
private final Lazy<String> findAllSql = Lazy.of(this::createFindAllSql);
@@ -64,17 +64,17 @@ class SqlGenerator {
6464
this.context = context;
6565
this.entity = entity;
6666
this.sqlGeneratorSource = sqlGeneratorSource;
67-
initPropertyNames();
67+
initColumnNames();
6868
}
6969

70-
private void initPropertyNames() {
70+
private void initColumnNames() {
7171

7272
entity.doWithProperties((PropertyHandler<JdbcPersistentProperty>) p -> {
7373
// the referencing column of referenced entity is expected to be on the other side of the relation
7474
if (!p.isEntity()) {
75-
propertyNames.add(p.getName());
75+
columnNames.add(p.getColumnName());
7676
if (!entity.isIdProperty(p)) {
77-
nonIdPropertyNames.add(p.getName());
77+
nonIdColumnNames.add(p.getColumnName());
7878
}
7979
}
8080
});
@@ -211,11 +211,11 @@ private String createInsertSql(boolean excludeId, Set<String> additionalColumns)
211211

212212
String insertTemplate = "insert into %s (%s) values (%s)";
213213

214-
List<String> propertyNamesForInsert = new ArrayList<>(excludeId ? nonIdPropertyNames : propertyNames);
215-
propertyNamesForInsert.addAll(additionalColumns);
214+
List<String> columnNamesForInsert = new ArrayList<>(excludeId ? nonIdColumnNames : columnNames);
215+
columnNamesForInsert.addAll(additionalColumns);
216216

217-
String tableColumns = String.join(", ", propertyNamesForInsert);
218-
String parameterNames = propertyNamesForInsert.stream().collect(Collectors.joining(", :", ":", ""));
217+
String tableColumns = String.join(", ", columnNamesForInsert);
218+
String parameterNames = columnNamesForInsert.stream().collect(Collectors.joining(", :", ":", ""));
219219

220220
return String.format(insertTemplate, entity.getTableName(), tableColumns, parameterNames);
221221
}
@@ -224,7 +224,7 @@ private String createUpdateSql() {
224224

225225
String updateTemplate = "update %s set %s where %s = :%s";
226226

227-
String setClause = propertyNames.stream()//
227+
String setClause = columnNames.stream()//
228228
.map(n -> String.format("%s = :%s", n, n))//
229229
.collect(Collectors.joining(", "));
230230

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.data.jdbc.mapping.model.DefaultNamingStrategy;
2727
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
2828
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity;
29+
import org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty;
2930
import org.springframework.data.jdbc.mapping.model.NamingStrategy;
3031
import org.springframework.data.mapping.PropertyPath;
3132

@@ -42,7 +43,7 @@ public class SqlGeneratorUnitTests {
4243
@Before
4344
public void setUp() {
4445

45-
NamingStrategy namingStrategy = new DefaultNamingStrategy();
46+
NamingStrategy namingStrategy = new PrefixingNamingStrategy();
4647
JdbcMappingContext context = new JdbcMappingContext(namingStrategy);
4748
JdbcPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(DummyEntity.class);
4849
this.sqlGenerator = new SqlGenerator(context, persistentEntity, new SqlGeneratorSource(context));
@@ -56,10 +57,10 @@ public void findOne() {
5657
SoftAssertions softAssertions = new SoftAssertions();
5758
softAssertions.assertThat(sql) //
5859
.startsWith("SELECT") //
59-
.contains("DummyEntity.id AS id,") //
60-
.contains("DummyEntity.name AS name,") //
61-
.contains("ref.l1id AS ref_l1id") //
62-
.contains("ref.content AS ref_content").contains(" FROM DummyEntity") //
60+
.contains("DummyEntity.x_id AS x_id,") //
61+
.contains("DummyEntity.x_name AS x_name,") //
62+
.contains("ref.x_l1id AS ref_x_l1id") //
63+
.contains("ref.x_content AS ref_x_content").contains(" FROM DummyEntity") //
6364
// 1-N relationships do not get loaded via join
6465
.doesNotContain("Element AS elements");
6566
softAssertions.assertAll();
@@ -79,7 +80,7 @@ public void cascadingDeleteAllSecondLevel() {
7980
String sql = sqlGenerator.createDeleteByPath(PropertyPath.from("ref.further", DummyEntity.class));
8081

8182
assertThat(sql).isEqualTo(
82-
"DELETE FROM SecondLevelReferencedEntity WHERE ReferencedEntity IN (SELECT l1id FROM ReferencedEntity WHERE DummyEntity = :rootId)");
83+
"DELETE FROM SecondLevelReferencedEntity WHERE ReferencedEntity IN (SELECT x_l1id FROM ReferencedEntity WHERE DummyEntity = :rootId)");
8384
}
8485

8586
@Test // DATAJDBC-112
@@ -104,7 +105,7 @@ public void cascadingDeleteSecondLevel() {
104105
String sql = sqlGenerator.createDeleteAllSql(PropertyPath.from("ref.further", DummyEntity.class));
105106

106107
assertThat(sql).isEqualTo(
107-
"DELETE FROM SecondLevelReferencedEntity WHERE ReferencedEntity IN (SELECT l1id FROM ReferencedEntity WHERE DummyEntity IS NOT NULL)");
108+
"DELETE FROM SecondLevelReferencedEntity WHERE ReferencedEntity IN (SELECT x_l1id FROM ReferencedEntity WHERE DummyEntity IS NOT NULL)");
108109
}
109110

110111
@SuppressWarnings("unused")
@@ -135,4 +136,13 @@ static class Element {
135136
@Id Long id;
136137
String content;
137138
}
139+
140+
private static class PrefixingNamingStrategy extends DefaultNamingStrategy {
141+
142+
@Override
143+
public String getColumnName(JdbcPersistentProperty property) {
144+
return "x_" + super.getColumnName(property);
145+
}
146+
147+
}
138148
}

src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ Class<?> testClass() {
6363
DummyEntityRepository dummyEntityRepository() {
6464
return factory.getRepository(DummyEntityRepository.class);
6565
}
66+
6667
}
6768

69+
6870
@ClassRule public static final SpringClassRule classRule = new SpringClassRule();
6971
@Rule public SpringMethodRule methodRule = new SpringMethodRule();
7072

src/test/java/org/springframework/data/jdbc/testing/TestConfiguration.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.data.jdbc.core.SqlGeneratorSource;
2828
import org.springframework.data.jdbc.mapping.model.DefaultNamingStrategy;
2929
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
30+
import org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty;
3031
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory;
3132
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
3233
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@@ -49,7 +50,12 @@ public class TestConfiguration {
4950
@Bean
5051
JdbcRepositoryFactory jdbcRepositoryFactory() {
5152

52-
final JdbcMappingContext context = new JdbcMappingContext(new DefaultNamingStrategy());
53+
final JdbcMappingContext context = new JdbcMappingContext(new DefaultNamingStrategy(){
54+
@Override
55+
public String getColumnName(JdbcPersistentProperty property) {
56+
return super.getColumnName(property);
57+
}
58+
});
5359

5460
return new JdbcRepositoryFactory( //
5561
publisher, //

0 commit comments

Comments
 (0)