Skip to content

Commit b9413bf

Browse files
committed
Fix assemble table alias for embedded entity with empty prefix and reference
1 parent 6e2d5ef commit b9413bf

File tree

2 files changed

+60
-19
lines changed

2 files changed

+60
-19
lines changed

spring-data-jdbc-plus-sql/src/main/java/com/navercorp/spring/data/jdbc/plus/sql/convert/PropertyPathUtils.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.data.relational.core.sql.IdentifierProcessing;
2323
import org.springframework.data.relational.core.sql.SqlIdentifier;
2424
import org.springframework.util.Assert;
25+
import org.springframework.util.StringUtils;
2526

2627
/**
2728
* PropertyPathUtils to get ColumnAlias and TableAlias applied @SqlTableAlias
@@ -110,20 +111,24 @@ private static PersistentPropertyPathExtension getTableOwningAncestor(Persistent
110111
return path.isEntity() && !path.isEmbedded() ? path : getTableOwningAncestor(path.getParentPath());
111112
}
112113

114+
@Nullable
113115
private static SqlIdentifier assembleTableAlias(PersistentPropertyPathExtension path) {
114116

115117
Assert.state(path != null, "Path is null");
116118

117119
String prefix = TableAliasUtils.getTableAliasPropertyPathPrefix(path);
118120
if (path.getLength() == 1) {
119121
Assert.notNull(prefix, "Prefix must not be null.");
120-
return SqlIdentifier.quoted(prefix);
122+
return StringUtils.hasText(prefix) ? SqlIdentifier.quoted(prefix) : null;
121123
}
122124

123125
PersistentPropertyPathExtension parentPath = path.getParentPath();
124126
SqlIdentifier sqlIdentifier = assembleTableAlias(parentPath);
125127

126-
return parentPath.isEmbedded() ? sqlIdentifier.transform(name -> name.concat(prefix))
127-
: sqlIdentifier.transform(name -> name + "_" + prefix);
128+
if (sqlIdentifier != null) {
129+
return parentPath.isEmbedded() ? sqlIdentifier.transform(name -> name.concat(prefix))
130+
: sqlIdentifier.transform(name -> name + "_" + prefix);
131+
}
132+
return SqlIdentifier.quoted(prefix);
128133
}
129134
}

spring-data-jdbc-plus-sql/src/test/java/com/navercorp/spring/data/jdbc/plus/sql/convert/SqlGeneratorEmbeddedTest.java

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import static java.util.Collections.*;
44
import static org.assertj.core.api.Assertions.*;
55
import static org.springframework.data.relational.core.mapping.Embedded.*;
6+
import static org.springframework.data.relational.core.sql.SqlIdentifier.quoted;
7+
import static org.springframework.data.relational.core.sql.SqlIdentifier.unquoted;
68

79
import org.assertj.core.api.SoftAssertions;
810
import org.junit.jupiter.api.BeforeEach;
@@ -12,11 +14,13 @@
1214
import org.springframework.data.jdbc.core.convert.BasicJdbcConverter;
1315
import org.springframework.data.jdbc.core.convert.JdbcConverter;
1416
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
17+
import org.springframework.data.mapping.PersistentPropertyPath;
1518
import org.springframework.data.relational.core.mapping.Column;
1619
import org.springframework.data.relational.core.mapping.Embedded;
1720
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
1821
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
1922
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
23+
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
2024
import org.springframework.data.relational.core.sql.Aliased;
2125
import org.springframework.data.relational.core.sql.SqlIdentifier;
2226

@@ -195,10 +199,10 @@ public void columnForEmbeddedProperty() {
195199
c -> getAlias(c.getTable()), //
196200
this::getAlias) //
197201
.containsExactly( //
198-
SqlIdentifier.unquoted("test"), //
199-
SqlIdentifier.unquoted("dummy_entity"), //
202+
unquoted("test"), //
203+
unquoted("dummy_entity"), //
200204
null, //
201-
SqlIdentifier.unquoted("test"));
205+
unquoted("test"));
202206
}
203207

204208
@Test // DATAJDBC-340
@@ -226,10 +230,10 @@ public void columnForPrefixedEmbeddedProperty() {
226230
c -> getAlias(c.getTable()), //
227231
this::getAlias) //
228232
.containsExactly( //
229-
SqlIdentifier.unquoted("prefix_test"), //
230-
SqlIdentifier.unquoted("dummy_entity"), //
233+
unquoted("prefix_test"), //
234+
unquoted("dummy_entity"), //
231235
null, //
232-
SqlIdentifier.unquoted("prefix_test"));
236+
unquoted("prefix_test"));
233237
}
234238

235239
@Test // DATAJDBC-340
@@ -249,8 +253,8 @@ public void columnForCascadedEmbeddedProperty() {
249253
c -> c.getTable().getName(),
250254
c -> getAlias(c.getTable()),
251255
this::getAlias)
252-
.containsExactly(SqlIdentifier.unquoted("attr1"), SqlIdentifier.unquoted("dummy_entity"), null,
253-
SqlIdentifier.unquoted("attr1"));
256+
.containsExactly(unquoted("attr1"), unquoted("dummy_entity"), null,
257+
unquoted("attr1"));
254258
}
255259

256260
@Test // DATAJDBC-340
@@ -261,14 +265,14 @@ public void joinForEmbeddedWithReference() {
261265
SoftAssertions.assertSoftly(softly -> {
262266

263267
softly.assertThat(join.getJoinTable().getName())
264-
.isEqualTo(SqlIdentifier.unquoted("other_entity"));
268+
.isEqualTo(unquoted("other_entity"));
265269
softly.assertThat(join.getJoinColumn().getTable()).isEqualTo(join.getJoinTable());
266270
softly.assertThat(join.getJoinColumn().getName())
267-
.isEqualTo(SqlIdentifier.unquoted("dummy_entity2"));
271+
.isEqualTo(unquoted("dummy_entity2"));
268272
softly.assertThat(join.getParentId().getName())
269-
.isEqualTo(SqlIdentifier.unquoted("id"));
273+
.isEqualTo(unquoted("id"));
270274
softly.assertThat(join.getParentId().getTable().getName())
271-
.isEqualTo(SqlIdentifier.unquoted("dummy_entity2"));
275+
.isEqualTo(unquoted("dummy_entity2"));
272276
});
273277
}
274278

@@ -282,10 +286,20 @@ public void columnForEmbeddedWithReferenceProperty() {
282286
c -> getAlias(c.getTable()), //
283287
this::getAlias) //
284288
.containsExactly( //
285-
SqlIdentifier.unquoted("value"), //
286-
SqlIdentifier.unquoted("other_entity"), //
287-
SqlIdentifier.quoted("prefix_other"), //
288-
SqlIdentifier.unquoted("prefix_other_value"));
289+
unquoted("value"), //
290+
unquoted("other_entity"), //
291+
quoted("prefix_other"), //
292+
unquoted("prefix_other_value"));
293+
}
294+
295+
@Test
296+
public void getTableAlias() {
297+
SoftAssertions.assertSoftly(softly -> {
298+
softly.assertThat(PropertyPathUtils.getTableAlias(extPath("prefixEmbeddable.other")))
299+
.isEqualTo(quoted("prefix_other"));
300+
softly.assertThat(PropertyPathUtils.getTableAlias(extPath("embeddable.other")))
301+
.isEqualTo(quoted("other"));
302+
});
289303
}
290304

291305
private SqlGenerator.Join generateJoin(String path, Class<?> type) {
@@ -303,6 +317,17 @@ private SqlIdentifier getAlias(Object maybeAliased) {
303317
return null;
304318
}
305319

320+
private PersistentPropertyPathExtension extPath(RelationalPersistentEntity<?> entity) {
321+
return new PersistentPropertyPathExtension(context, entity);
322+
}
323+
private PersistentPropertyPathExtension extPath(String path) {
324+
return new PersistentPropertyPathExtension(context, createSimplePath(path));
325+
}
326+
327+
PersistentPropertyPath<RelationalPersistentProperty> createSimplePath(String path) {
328+
return PropertyPathTestingUtils.toPath(path, DummyEntity3.class, context);
329+
}
330+
306331
private org.springframework.data.relational.core.sql.Column generatedColumn(
307332
String path, Class<?> type) {
308333

@@ -350,6 +375,17 @@ static class DummyEntity2 {
350375
EmbeddedWithReference embedded;
351376
}
352377

378+
static class DummyEntity3 {
379+
@Id
380+
Long id;
381+
382+
@Embedded(onEmpty = OnEmpty.USE_NULL, prefix = "prefix_")
383+
EmbeddedWithReference prefixEmbeddable;
384+
385+
@Embedded(onEmpty = OnEmpty.USE_NULL)
386+
EmbeddedWithReference embeddable;
387+
}
388+
353389
static class EmbeddedWithReference {
354390
OtherEntity other;
355391
}

0 commit comments

Comments
 (0)