diff --git a/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java b/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java index 645a96c5e..a60b24eed 100644 --- a/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java +++ b/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java @@ -224,9 +224,17 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty getAssociationTargetType() { - return associationTargetType != null // - ? associationTargetType.getType() // - : super.getAssociationTargetType(); + if (!isAssociation()) { + return null; + } + + if (associationTargetType != null) { + return associationTargetType.getType(); + } + + Class targetType = super.getAssociationTargetType(); + + return targetType != null ? targetType : getActualType(); } /** diff --git a/src/test/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImplUnitTests.java b/src/test/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImplUnitTests.java index 28cf1a220..1a4f219c8 100644 --- a/src/test/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImplUnitTests.java +++ b/src/test/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImplUnitTests.java @@ -41,7 +41,6 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; - import org.springframework.data.annotation.AccessType.Type; import org.springframework.data.annotation.Version; import org.springframework.data.util.ClassTypeInformation; @@ -74,7 +73,9 @@ public class JpaPersistentPropertyImplUnitTests { void considersOneToOneMappedPropertyAnAssociation() { JpaPersistentProperty property = entity.getRequiredPersistentProperty("other"); + assertThat(property.isAssociation()).isTrue(); + assertThat(property.getAssociationTargetType()).isEqualTo(Sample.class); } @Test // DATAJPA-376 @@ -146,7 +147,7 @@ public class JpaPersistentPropertyImplUnitTests { Iterable> entityType = property.getPersistentEntityTypes(); assertThat(entityType.iterator().hasNext()).isTrue(); assertThat(entityType.iterator().next()) - .isEqualTo((TypeInformation) ClassTypeInformation.from(Implementation.class)); + .isEqualTo(ClassTypeInformation.from(Implementation.class)); } @Test // DATAJPA-716