From c643d2d90f70c9772cc33c0ab0d0e10e206f2f10 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Wed, 7 Apr 2021 15:16:41 +0200 Subject: [PATCH] =?UTF-8?q?AbstractPersistentProperty=20now=20considers=20?= =?UTF-8?q?association=20target=20type=20for=20=E2=80=A6.getPersistentEnti?= =?UTF-8?q?tyTypes().?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prior to this commit, the lookup of types to be added as persistent entities for a given PersistentProperty used the actual property type which did not consider the association target type at all. Removed the usage of Optional for the entity type information and switched to use TypeInformation for the raw association target type so that it can be considered in ….getActualType(). --- .../model/AbstractPersistentProperty.java | 54 +++++++++++++------ .../AbstractPersistentPropertyUnitTests.java | 3 ++ 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java b/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java index 49618a8a2..836984db3 100644 --- a/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java @@ -21,6 +21,7 @@ import java.lang.reflect.Modifier; import java.util.Collections; import java.util.Map; import java.util.Optional; +import java.util.function.Supplier; import org.springframework.data.mapping.Association; import org.springframework.data.mapping.PersistentEntity; @@ -56,15 +57,13 @@ public abstract class AbstractPersistentProperty

private final Class rawType; private final Lazy> association; private final PersistentEntity owner; - - @SuppressWarnings("null") // private final Property property; private final Lazy hashCode; private final Lazy usePropertyAccess; - private final Lazy>> entityTypeInformation; + private final Lazy> entityTypeInformation; private final Lazy isAssociation; - private final Lazy> associationTargetType; + private final Lazy> associationTargetType; private final Method getter; private final Method setter; @@ -94,13 +93,13 @@ public abstract class AbstractPersistentProperty

: Lazy.of(() -> Optional.of(getTypeInformation()) .map(it -> it.getSuperTypeInformation(ASSOCIATION_TYPE)) .map(TypeInformation::getComponentType) - .map(TypeInformation::getType) .orElse(null)); - this.entityTypeInformation = Lazy.of(() -> Optional.ofNullable(information.getActualType())// - .filter(it -> !simpleTypeHolder.isSimpleType(it.getType()))// - .filter(it -> !it.isCollectionLike())// - .filter(it -> !it.isMap())); + this.entityTypeInformation = Lazy.of(() -> Optional.ofNullable(getAssociationOrActualType()) + .filter(it -> !simpleTypeHolder.isSimpleType(it.getType())) // + .filter(it -> !it.isCollectionLike()) // + .filter(it -> !it.isMap()) + .orElse(null)); this.getter = property.getGetter().orElse(null); this.setter = property.getSetter().orElse(null); @@ -172,9 +171,9 @@ public abstract class AbstractPersistentProperty

return Collections.emptySet(); } - return entityTypeInformation.get()// - .map(Collections::singleton)// - .orElseGet(Collections::emptySet); + TypeInformation result = getAssociationTypeOr(() -> entityTypeInformation.getNullable()); + + return result != null ? Collections.singleton(result) : Collections.emptySet(); } /* @@ -279,7 +278,10 @@ public abstract class AbstractPersistentProperty

@Nullable @Override public Class getAssociationTargetType() { - return associationTargetType.getNullable(); + + TypeInformation result = associationTargetType.getNullable(); + + return result != null ? result.getType() : null; } /* @@ -315,7 +317,7 @@ public abstract class AbstractPersistentProperty

*/ @Override public boolean isEntity() { - return !isTransient() && entityTypeInformation.get().isPresent(); + return !isTransient() && entityTypeInformation.getNullable() != null; } /* @@ -339,6 +341,7 @@ public abstract class AbstractPersistentProperty

if (isMap()) { TypeInformation mapValueType = information.getMapValueType(); + if (mapValueType != null) { return mapValueType.getType(); } @@ -353,7 +356,7 @@ public abstract class AbstractPersistentProperty

*/ @Override public Class getActualType() { - return information.getRequiredActualType().getType(); + return getRequiredAssociationOrActualType().getType(); } /* @@ -364,7 +367,6 @@ public abstract class AbstractPersistentProperty

return usePropertyAccess.get(); } - @SuppressWarnings("null") protected Property getProperty() { return this.property; } @@ -406,4 +408,24 @@ public abstract class AbstractPersistentProperty

public String toString() { return property.toString(); } + + @Nullable + private TypeInformation getAssociationOrActualType() { + return getAssociationTypeOr(() -> information.getActualType()); + } + + private TypeInformation getRequiredAssociationOrActualType() { + return getAssociationTypeOr(() -> information.getRequiredActualType()); + } + + private TypeInformation getAssociationTypeOr(Supplier> fallback) { + + TypeInformation result = associationTargetType.getNullable(); + + if (result != null) { + return result; + } + + return fallback.get(); + } } diff --git a/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java b/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java index 06cd5300b..d9092f52d 100755 --- a/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java @@ -233,6 +233,9 @@ public class AbstractPersistentPropertyUnitTests { assertThat(property.isAssociation()).isTrue(); assertThat(property.getAssociationTargetType()).isEqualTo(JMoleculesAggregate.class); + assertThat(property.getPersistentEntityTypes()) + .extracting(it -> it.getType()) + .containsExactly((Class) JMoleculesAggregate.class); } private BasicPersistentEntity getEntity(Class type) {