diff --git a/src/main/java/org/springframework/data/mapping/PersistentProperty.java b/src/main/java/org/springframework/data/mapping/PersistentProperty.java index 150ba628f..acffe7cd2 100644 --- a/src/main/java/org/springframework/data/mapping/PersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/PersistentProperty.java @@ -70,9 +70,21 @@ public interface PersistentProperty

> { * {@link Map}'s value type transparently. * * @return + * @deprecated since 2.6 for removal in 3.0. Use {@link #getPersistentEntityTypeInformation()} instead. */ + @Deprecated Iterable> getPersistentEntityTypes(); + /** + * Returns the {@link TypeInformation} if the property references a {@link PersistentEntity}. Will return + * {@literal null} in case it refers to a simple type. Will return {@link Collection}'s component type or the + * {@link Map}'s value type transparently. + * + * @return + * @since 2.6 + */ + Iterable> getPersistentEntityTypeInformation(); + /** * Returns the getter method to access the property value if available. Might return {@literal null} in case there is * no getter method with a return type assignable to the actual property's type. @@ -395,6 +407,19 @@ public interface PersistentProperty

> { @Nullable Class getAssociationTargetType(); + /** + * Return the type the property refers to in case it's an association, i.e. {@link #isAssociation()} returns + * {@literal true}. That means, that implementations must return a non-{@literal null} value from this method + * in that case. We also recommend to return {@literal null} for non-associations right away to establish symmetry + * between this method and {@link #isAssociation()}. + * + * @return the type the property refers to in case it's an association, i.e. {@link #isAssociation()} returns + * {@literal true}. + * @since 2.6 + */ + @Nullable + TypeInformation getAssociationTargetTypeInformation(); + /** * Returns a {@link PersistentPropertyAccessor} for the current property's owning value. * diff --git a/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java b/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java index 6bd393541..04c5c21d6 100644 --- a/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java +++ b/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java @@ -33,7 +33,6 @@ import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import org.springframework.beans.BeanUtils; import org.springframework.beans.BeansException; import org.springframework.beans.factory.InitializingBean; @@ -576,7 +575,7 @@ public abstract class AbstractMappingContext { + property.getPersistentEntityTypeInformation().forEach(it -> { if (shouldCreatePersistentEntityFor(it)) { addPersistentEntity(it); 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 fb30c1387..fa3a9e692 100644 --- a/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java @@ -164,6 +164,15 @@ public abstract class AbstractPersistentProperty

*/ @Override public Iterable> getPersistentEntityTypes() { + return getPersistentEntityTypeInformation(); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mapping.PersistentProperty#getPersistentEntityTypeInformation() + */ + @Override + public Iterable> getPersistentEntityTypeInformation() { if (isMap() || isCollectionLike()) { return entityTypeInformation.get(); @@ -279,11 +288,21 @@ public abstract class AbstractPersistentProperty

@Override public Class getAssociationTargetType() { - TypeInformation result = associationTargetType.getNullable(); + TypeInformation result = getAssociationTargetTypeInformation(); return result != null ? result.getType() : null; } + /* + * (non-Javadoc) + * @see org.springframework.data.mapping.PersistentProperty#getAssociationTargetTypeInformation() + */ + @Nullable + @Override + public TypeInformation getAssociationTargetTypeInformation() { + return associationTargetType.getNullable(); + } + /* * (non-Javadoc) * @see org.springframework.data.mapping.PersistentProperty#isCollectionLike() @@ -356,11 +375,7 @@ public abstract class AbstractPersistentProperty

*/ @Override public Class getActualType() { - - TypeInformation targetType = associationTargetType.getNullable(); - TypeInformation result = targetType == null ? information.getRequiredActualType() : targetType; - - return result.getType(); + return getActualTypeInformation().getType(); } /* @@ -375,6 +390,12 @@ public abstract class AbstractPersistentProperty

return this.property; } + protected TypeInformation getActualTypeInformation() { + + TypeInformation targetType = associationTargetType.getNullable(); + return targetType == null ? information.getRequiredActualType() : targetType; + } + /* * (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) diff --git a/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java b/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java index 52e5392ee..2be985377 100644 --- a/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java @@ -37,9 +37,11 @@ import org.springframework.data.mapping.Association; import org.springframework.data.mapping.MappingException; import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PersistentProperty; +import org.springframework.data.util.ClassTypeInformation; import org.springframework.data.util.Lazy; import org.springframework.data.util.Optionals; import org.springframework.data.util.StreamUtils; +import org.springframework.data.util.TypeInformation; import org.springframework.lang.Nullable; import org.springframework.util.Assert; @@ -74,7 +76,7 @@ public abstract class AnnotationBasedPersistentProperty

isId = Lazy.of(() -> isAnnotationPresent(Id.class)); private final Lazy isVersion = Lazy.of(() -> isAnnotationPresent(Version.class)); - private final Lazy> associationTargetType = Lazy.of(() -> { + private final Lazy> associationTargetType = Lazy.of(() -> { if (!isAssociation()) { return null; @@ -83,8 +85,8 @@ public abstract class AnnotationBasedPersistentProperty

!Class.class.equals(it) ? it : getActualType()) // - .orElseGet(() -> super.getAssociationTargetType()); + .map(it -> !Class.class.equals(it) ? ClassTypeInformation.from(it) : getActualTypeInformation()) // + .orElseGet(() -> super.getAssociationTargetTypeInformation()); }); /** @@ -295,11 +297,11 @@ public abstract class AnnotationBasedPersistentProperty

getAssociationTargetType() { + public TypeInformation getAssociationTargetTypeInformation() { return associationTargetType.getNullable(); } diff --git a/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextIntegrationTests.java b/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextIntegrationTests.java index aa0e18b3b..61967c2a7 100755 --- a/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextIntegrationTests.java +++ b/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextIntegrationTests.java @@ -113,7 +113,7 @@ class AbstractMappingContextIntegrationTests> { when(prop.getTypeInformation()).thenReturn(owner.getTypeInformation()); when(prop.getName()).thenReturn(property.getName()); - when(prop.getPersistentEntityTypes()).thenReturn(Collections.EMPTY_SET); + when(prop.getPersistentEntityTypeInformation()).thenReturn(Collections.EMPTY_SET); try { Thread.sleep(200); 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 d9092f52d..f9d11f075 100755 --- a/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java @@ -73,7 +73,7 @@ public class AbstractPersistentPropertyUnitTests { @Test // DATACMNS-101 void returnsNestedEntityTypeCorrectly() { - assertThat(getProperty(TestClassComplex.class, "testClassSet").getPersistentEntityTypes()).isEmpty(); + assertThat(getProperty(TestClassComplex.class, "testClassSet").getPersistentEntityTypeInformation()).isEmpty(); } @Test // DATACMNS-132 @@ -198,7 +198,7 @@ public class AbstractPersistentPropertyUnitTests { void doesNotConsiderPropertyWithTreeMapMapValueAnEntity() { SamplePersistentProperty property = getProperty(TreeMapWrapper.class, "map"); - assertThat(property.getPersistentEntityTypes()).isEmpty(); + assertThat(property.getPersistentEntityTypeInformation()).isEmpty(); assertThat(property.isEntity()).isFalse(); } @@ -233,7 +233,7 @@ public class AbstractPersistentPropertyUnitTests { assertThat(property.isAssociation()).isTrue(); assertThat(property.getAssociationTargetType()).isEqualTo(JMoleculesAggregate.class); - assertThat(property.getPersistentEntityTypes()) + assertThat(property.getPersistentEntityTypeInformation()) .extracting(it -> it.getType()) .containsExactly((Class) JMoleculesAggregate.class); }