From e6c370be2da95ad3e938b63fb5725a4da3838db5 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Wed, 7 Apr 2021 16:23:05 +0200 Subject: [PATCH] JpaPersistentPropertyImpl.getAssociationTargetType() falls back to actual type for associations In case a property is an association we now fall back to the property's actual type if there's no explicit association target type. --- .../jpa/mapping/JpaPersistentPropertyImpl.java | 14 +++++++++++--- .../JpaPersistentPropertyImplUnitTests.java | 5 +++-- 2 files changed, 14 insertions(+), 5 deletions(-) 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