Skip to content

Commit f21f031

Browse files
RazorNdschauder
authored andcommitted
Fix assemble table alias for embedded entity with empty prefix and reference.
Table alias for embedded entity use prefix value from Embedded annotation. But default value for prefix is empty string. If try to create SqlIdentifier for empty string it throw exception. To avoid this behavior, if prefix is an empty string, property name for embedded entity is taken instead.
1 parent 7c20658 commit f21f031

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

Diff for: spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/PersistentPropertyPathExtensionUnitTests.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2021 the original author or authors.
2+
* Copyright 2019-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232

3333
/**
3434
* @author Jens Schauder
35+
* @author Daniil Razorenov
3536
*/
3637
public class PersistentPropertyPathExtensionUnitTests {
3738

@@ -126,6 +127,7 @@ void getTableAlias() {
126127
softly.assertThat(extPath("secondList.third.value").getTableAlias()).isEqualTo(quoted("secondList_third"));
127128
softly.assertThat(extPath("secondList").getTableAlias()).isEqualTo(quoted("secondList"));
128129
softly.assertThat(extPath("second2.third").getTableAlias()).isEqualTo(quoted("secthird"));
130+
softly.assertThat(extPath("second3.third").getTableAlias()).isEqualTo(quoted("second3third"));
129131
});
130132
}
131133

@@ -237,6 +239,7 @@ static class DummyEntity {
237239
@Id Long entityId;
238240
Second second;
239241
@Embedded(onEmpty = OnEmpty.USE_NULL, prefix = "sec") Second second2;
242+
@Embedded(onEmpty = OnEmpty.USE_NULL) Second second3;
240243
List<Second> secondList;
241244
WithId withId;
242245
}

Diff for: spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryEmbeddedWithReferenceIntegrationTests.java

+8
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ private static class DummyEntity {
277277
String test;
278278

279279
@Embedded(onEmpty = OnEmpty.USE_NULL, prefix = "PREFIX_") Embeddable embeddable;
280+
281+
@Embedded(onEmpty = OnEmpty.USE_NULL) Embeddable2 embeddable2;
280282
}
281283

282284
@Data
@@ -287,6 +289,12 @@ private static class Embeddable {
287289
String test;
288290
}
289291

292+
@Data
293+
private static class Embeddable2 {
294+
295+
@Column("ID") DummyEntity2 dummyEntity2;
296+
}
297+
290298
@Data
291299
private static class DummyEntity2 {
292300

Diff for: spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/PersistentPropertyPathExtension.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* available used in SQL generation and conversion
3232
*
3333
* @author Jens Schauder
34+
* @author Daniil Razorenov
3435
* @since 1.1
3536
*/
3637
public class PersistentPropertyPathExtension {
@@ -389,7 +390,12 @@ private SqlIdentifier assembleTableAlias() {
389390
Assert.state(path != null, "Path is null");
390391

391392
RelationalPersistentProperty leafProperty = path.getRequiredLeafProperty();
392-
String prefix = isEmbedded() ? leafProperty.getEmbeddedPrefix() : leafProperty.getName();
393+
String prefix;
394+
if (isEmbedded() && (leafProperty.getEmbeddedPrefix() == null || !leafProperty.getEmbeddedPrefix().isEmpty())) {
395+
prefix = leafProperty.getEmbeddedPrefix();
396+
} else {
397+
prefix = leafProperty.getName();
398+
}
393399

394400
if (path.getLength() == 1) {
395401
Assert.notNull(prefix, "Prefix mus not be null.");

0 commit comments

Comments
 (0)