Browse Source

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
2.3.x
Jens Schauder 4 years ago
parent
commit
46a5eb6d76
No known key found for this signature in database
GPG Key ID: 45CC872F17423DBF
  1. 2
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/PersistentPropertyPathExtensionUnitTests.java
  2. 24
      spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/PersistentPropertyPathExtension.java

2
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/PersistentPropertyPathExtensionUnitTests.java

@ -127,7 +127,7 @@ public class PersistentPropertyPathExtensionUnitTests {
softly.assertThat(extPath("secondList.third.value").getTableAlias()).isEqualTo(quoted("secondList_third")); softly.assertThat(extPath("secondList.third.value").getTableAlias()).isEqualTo(quoted("secondList_third"));
softly.assertThat(extPath("secondList").getTableAlias()).isEqualTo(quoted("secondList")); softly.assertThat(extPath("secondList").getTableAlias()).isEqualTo(quoted("secondList"));
softly.assertThat(extPath("second2.third").getTableAlias()).isEqualTo(quoted("secthird")); 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"));
}); });
} }

24
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; package org.springframework.data.relational.core.mapping;
import java.util.Objects;
import org.springframework.data.mapping.PersistentProperty; import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PersistentPropertyPath; import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mapping.context.MappingContext;
@ -23,8 +25,7 @@ import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.data.util.Lazy; import org.springframework.data.util.Lazy;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import java.util.Objects;
/** /**
* A wrapper around a {@link org.springframework.data.mapping.PersistentPropertyPath} for making common operations * A wrapper around a {@link org.springframework.data.mapping.PersistentPropertyPath} for making common operations
@ -385,28 +386,34 @@ public class PersistentPropertyPathExtension {
return isEntity() && !isEmbedded() ? this : getParentPath().getTableOwningAncestor(); return isEntity() && !isEmbedded() ? this : getParentPath().getTableOwningAncestor();
} }
@Nullable
private SqlIdentifier assembleTableAlias() { private SqlIdentifier assembleTableAlias() {
Assert.state(path != null, "Path is null"); Assert.state(path != null, "Path is null");
RelationalPersistentProperty leafProperty = path.getRequiredLeafProperty(); RelationalPersistentProperty leafProperty = path.getRequiredLeafProperty();
String prefix; String prefix;
if (isEmbedded() && (leafProperty.getEmbeddedPrefix() == null || !leafProperty.getEmbeddedPrefix().isEmpty())) { if (isEmbedded()) {
prefix = leafProperty.getEmbeddedPrefix(); prefix = leafProperty.getEmbeddedPrefix();
} else { } else {
prefix = leafProperty.getName(); prefix = leafProperty.getName();
} }
if (path.getLength() == 1) { if (path.getLength() == 1) {
Assert.notNull(prefix, "Prefix mus not be null."); Assert.notNull(prefix, "Prefix mus not be null.");
return SqlIdentifier.quoted(prefix); return StringUtils.hasText(prefix) ? SqlIdentifier.quoted(prefix) : null;
} }
PersistentPropertyPathExtension parentPath = getParentPath(); PersistentPropertyPathExtension parentPath = getParentPath();
SqlIdentifier sqlIdentifier = parentPath.assembleTableAlias(); SqlIdentifier sqlIdentifier = parentPath.assembleTableAlias();
if (sqlIdentifier != null) {
return parentPath.isEmbedded() ? sqlIdentifier.transform(name -> name.concat(prefix)) return parentPath.isEmbedded() ? sqlIdentifier.transform(name -> name.concat(prefix))
: sqlIdentifier.transform(name -> name + "_" + prefix); : sqlIdentifier.transform(name -> name + "_" + prefix);
}
return SqlIdentifier.quoted(prefix);
} }
@ -444,11 +451,12 @@ public class PersistentPropertyPathExtension {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o)
if (o == null || getClass() != o.getClass()) return false; return true;
if (o == null || getClass() != o.getClass())
return false;
PersistentPropertyPathExtension that = (PersistentPropertyPathExtension) o; PersistentPropertyPathExtension that = (PersistentPropertyPathExtension) o;
return entity.equals(that.entity) && return entity.equals(that.entity) && Objects.equals(path, that.path);
Objects.equals(path, that.path);
} }
@Override @Override

Loading…
Cancel
Save