Persistent entity type detection now starts with the association target type if available.

This causes us to inspect the most concrete type we can find from a property declaration that might be overridden using an annotation.

Closes: #2409
Original Pull Request: #2410
This commit is contained in:
Oliver Drotbohm
2021-07-09 15:09:25 +02:00
committed by Christoph Strobl
parent e565c11dcc
commit d3cd1ab60a
2 changed files with 13 additions and 3 deletions

View File

@@ -47,6 +47,7 @@ import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.context.SampleMappingContext;
import org.springframework.data.mapping.context.SamplePersistentProperty;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.lang.Nullable;
import org.springframework.test.util.ReflectionTestUtils;
@@ -318,6 +319,16 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase
assertThat(property.getAssociationTargetType()).isEqualTo(JMoleculesAggregate.class);
}
@Test // GH-2409
void exposesAssociationTargetClassAsPersistentEntityType() {
SamplePersistentProperty property = getProperty(WithReferences.class, "toSample");
assertThat(property.getPersistentEntityTypeInformation()) //
.isNotEmpty() //
.allMatch(it -> it.equals(ClassTypeInformation.from(Sample.class)));
}
@SuppressWarnings("unchecked")
private Map<Class<? extends Annotation>, Annotation> getAnnotationCache(SamplePersistentProperty property) {
return (Map<Class<? extends Annotation>, Annotation>) ReflectionTestUtils.getField(property, "annotationCache");