Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
schauder committed Mar 23, 2022
1 parent f21f031 commit 906f75c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);

}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 906f75c

Please sign in to comment.