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 extends TypeInformation>> 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 extends TypeInformation>> 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
*/
@Override
public Iterable extends TypeInformation>> getPersistentEntityTypes() {
+ return getPersistentEntityTypeInformation();
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.mapping.PersistentProperty#getPersistentEntityTypeInformation()
+ */
+ @Override
+ public Iterable extends TypeInformation>> 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 !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