From 657e9cac65ad499622bc7fb2b5354c00e86fc8c7 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Wed, 23 Mar 2022 15:31:03 +0100 Subject: [PATCH] Polishing. Changed default behaviour to an empty name for embedded entities. This allows to use embedded entities for column tuples without special prefix. Original pull request #1149 --- ...sistentPropertyPathExtensionUnitTests.java | 2 +- .../PersistentPropertyPathExtension.java | 28 ++++++++++++------- 2 files changed, 19 insertions(+), 11 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 18a86a18b0..9751351a2d 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 @@ -127,7 +127,7 @@ void getTableAlias() { 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")); + softly.assertThat(extPath("second3.third").getTableAlias()).isEqualTo(quoted("third")); }); } 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 b6f316ead7..690128553a 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 @@ -15,6 +15,8 @@ */ package org.springframework.data.relational.core.mapping; +import java.util.Objects; + import org.springframework.data.mapping.PersistentProperty; import org.springframework.data.mapping.PersistentPropertyPath; import org.springframework.data.mapping.context.MappingContext; @@ -23,8 +25,7 @@ import org.springframework.data.util.Lazy; import org.springframework.lang.Nullable; import org.springframework.util.Assert; - -import java.util.Objects; +import org.springframework.util.StringUtils; /** * A wrapper around a {@link org.springframework.data.mapping.PersistentPropertyPath} for making common operations @@ -385,28 +386,34 @@ private PersistentPropertyPathExtension getTableOwningAncestor() { return isEntity() && !isEmbedded() ? this : getParentPath().getTableOwningAncestor(); } + @Nullable private SqlIdentifier assembleTableAlias() { Assert.state(path != null, "Path is null"); RelationalPersistentProperty leafProperty = path.getRequiredLeafProperty(); String prefix; - if (isEmbedded() && (leafProperty.getEmbeddedPrefix() == null || !leafProperty.getEmbeddedPrefix().isEmpty())) { + if (isEmbedded()) { prefix = leafProperty.getEmbeddedPrefix(); + } else { prefix = leafProperty.getName(); } if (path.getLength() == 1) { Assert.notNull(prefix, "Prefix mus not be null."); - return SqlIdentifier.quoted(prefix); + return StringUtils.hasText(prefix) ? SqlIdentifier.quoted(prefix) : null; } PersistentPropertyPathExtension parentPath = getParentPath(); SqlIdentifier sqlIdentifier = parentPath.assembleTableAlias(); - return parentPath.isEmbedded() ? sqlIdentifier.transform(name -> name.concat(prefix)) - : sqlIdentifier.transform(name -> name + "_" + prefix); + if (sqlIdentifier != null) { + + return parentPath.isEmbedded() ? sqlIdentifier.transform(name -> name.concat(prefix)) + : sqlIdentifier.transform(name -> name + "_" + prefix); + } + return SqlIdentifier.quoted(prefix); } @@ -444,11 +451,12 @@ private SqlIdentifier prefixWithTableAlias(SqlIdentifier columnName) { @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; PersistentPropertyPathExtension that = (PersistentPropertyPathExtension) o; - return entity.equals(that.entity) && - Objects.equals(path, that.path); + return entity.equals(that.entity) && Objects.equals(path, that.path); } @Override