Skip to content

Commit 2fb449c

Browse files
odrotbohmchristophstrobl
authored andcommitted
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. Fixes #2409.
1 parent 572bea0 commit 2fb449c

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,8 @@ public String toString() {
436436

437437
private Set<TypeInformation<?>> detectEntityTypes(SimpleTypeHolder simpleTypes) {
438438

439-
TypeInformation<?> typeToStartWith = ASSOCIATION_TYPE != null && ASSOCIATION_TYPE.isAssignableFrom(rawType)
440-
? information.getComponentType()
441-
: information;
439+
TypeInformation<?> typeToStartWith = getAssociationTargetTypeInformation();
440+
typeToStartWith = typeToStartWith == null ? information : typeToStartWith;
442441

443442
Set<TypeInformation<?>> result = detectEntityTypes(typeToStartWith);
444443

src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java

+11
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.springframework.data.mapping.PersistentProperty;
4848
import org.springframework.data.mapping.context.SampleMappingContext;
4949
import org.springframework.data.mapping.context.SamplePersistentProperty;
50+
import org.springframework.data.util.ClassTypeInformation;
5051
import org.springframework.lang.Nullable;
5152
import org.springframework.test.util.ReflectionTestUtils;
5253

@@ -318,6 +319,16 @@ void detectsJMoleculesAssociation() {
318319
assertThat(property.getAssociationTargetType()).isEqualTo(JMoleculesAggregate.class);
319320
}
320321

322+
@Test // GH-2409
323+
void exposesAssociationTargetClassAsPersistentEntityType() {
324+
325+
SamplePersistentProperty property = getProperty(WithReferences.class, "toSample");
326+
327+
assertThat(property.getPersistentEntityTypeInformation()) //
328+
.isNotEmpty() //
329+
.allMatch(it -> it.equals(ClassTypeInformation.from(Sample.class)));
330+
}
331+
321332
@SuppressWarnings("unchecked")
322333
private Map<Class<? extends Annotation>, Annotation> getAnnotationCache(SamplePersistentProperty property) {
323334
return (Map<Class<? extends Annotation>, Annotation>) ReflectionTestUtils.getField(property, "annotationCache");

0 commit comments

Comments
 (0)