From b53e4e6091b5d19c3989abf8d5c15063f38fb10e Mon Sep 17 00:00:00 2001 From: Daniil Razorenov Date: Sat, 29 Jan 2022 19:37:49 +0300 Subject: [PATCH] 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. --- .../core/PersistentPropertyPathExtensionUnitTests.java | 5 ++++- ...bcRepositoryEmbeddedWithReferenceIntegrationTests.java | 8 ++++++++ .../core/mapping/PersistentPropertyPathExtension.java | 8 +++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/PersistentPropertyPathExtensionUnitTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/PersistentPropertyPathExtensionUnitTests.java index be07ab8a..18a86a18 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/PersistentPropertyPathExtensionUnitTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/PersistentPropertyPathExtensionUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 the original author or authors. + * Copyright 2019-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,6 +32,7 @@ import org.springframework.data.relational.core.mapping.RelationalPersistentProp /** * @author Jens Schauder + * @author Daniil Razorenov */ public class PersistentPropertyPathExtensionUnitTests { @@ -126,6 +127,7 @@ public class PersistentPropertyPathExtensionUnitTests { softly.assertThat(extPath("secondList.third.value").getTableAlias()).isEqualTo(quoted("secondList_third")); softly.assertThat(extPath("secondList").getTableAlias()).isEqualTo(quoted("secondList")); softly.assertThat(extPath("second2.third").getTableAlias()).isEqualTo(quoted("secthird")); + softly.assertThat(extPath("second3.third").getTableAlias()).isEqualTo(quoted("second3third")); }); } @@ -237,6 +239,7 @@ public class PersistentPropertyPathExtensionUnitTests { @Id Long entityId; Second second; @Embedded(onEmpty = OnEmpty.USE_NULL, prefix = "sec") Second second2; + @Embedded(onEmpty = OnEmpty.USE_NULL) Second second3; List secondList; WithId withId; } diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryEmbeddedWithReferenceIntegrationTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryEmbeddedWithReferenceIntegrationTests.java index 0b850b7f..d396ba02 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryEmbeddedWithReferenceIntegrationTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryEmbeddedWithReferenceIntegrationTests.java @@ -277,6 +277,8 @@ public class JdbcRepositoryEmbeddedWithReferenceIntegrationTests { String test; @Embedded(onEmpty = OnEmpty.USE_NULL, prefix = "PREFIX_") Embeddable embeddable; + + @Embedded(onEmpty = OnEmpty.USE_NULL) Embeddable2 embeddable2; } @Data @@ -287,6 +289,12 @@ public class JdbcRepositoryEmbeddedWithReferenceIntegrationTests { String test; } + @Data + private static class Embeddable2 { + + @Column("ID") DummyEntity2 dummyEntity2; + } + @Data private static class DummyEntity2 { diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/PersistentPropertyPathExtension.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/PersistentPropertyPathExtension.java index dfb3f8ed..b6f316ea 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/PersistentPropertyPathExtension.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/PersistentPropertyPathExtension.java @@ -31,6 +31,7 @@ import java.util.Objects; * available used in SQL generation and conversion * * @author Jens Schauder + * @author Daniil Razorenov * @since 1.1 */ public class PersistentPropertyPathExtension { @@ -389,7 +390,12 @@ public class PersistentPropertyPathExtension { Assert.state(path != null, "Path is null"); RelationalPersistentProperty leafProperty = path.getRequiredLeafProperty(); - String prefix = isEmbedded() ? leafProperty.getEmbeddedPrefix() : leafProperty.getName(); + String prefix; + if (isEmbedded() && (leafProperty.getEmbeddedPrefix() == null || !leafProperty.getEmbeddedPrefix().isEmpty())) { + prefix = leafProperty.getEmbeddedPrefix(); + } else { + prefix = leafProperty.getName(); + } if (path.getLength() == 1) { Assert.notNull(prefix, "Prefix mus not be null.");