Skip to content

Commit e6c370b

Browse files
committed
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.
1 parent 582fa61 commit e6c370b

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,17 @@ public boolean isEmbeddable() {
224224
@Override
225225
public Class<?> getAssociationTargetType() {
226226

227-
return associationTargetType != null //
228-
? associationTargetType.getType() //
229-
: super.getAssociationTargetType();
227+
if (!isAssociation()) {
228+
return null;
229+
}
230+
231+
if (associationTargetType != null) {
232+
return associationTargetType.getType();
233+
}
234+
235+
Class<?> targetType = super.getAssociationTargetType();
236+
237+
return targetType != null ? targetType : getActualType();
230238
}
231239

232240
/**

src/test/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImplUnitTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import org.mockito.junit.jupiter.MockitoExtension;
4242
import org.mockito.junit.jupiter.MockitoSettings;
4343
import org.mockito.quality.Strictness;
44-
4544
import org.springframework.data.annotation.AccessType.Type;
4645
import org.springframework.data.annotation.Version;
4746
import org.springframework.data.util.ClassTypeInformation;
@@ -74,7 +73,9 @@ void setUp() {
7473
void considersOneToOneMappedPropertyAnAssociation() {
7574

7675
JpaPersistentProperty property = entity.getRequiredPersistentProperty("other");
76+
7777
assertThat(property.isAssociation()).isTrue();
78+
assertThat(property.getAssociationTargetType()).isEqualTo(Sample.class);
7879
}
7980

8081
@Test // DATAJPA-376
@@ -146,7 +147,7 @@ void considersTargetEntityTypeForPropertyType() {
146147
Iterable<? extends TypeInformation<?>> entityType = property.getPersistentEntityTypes();
147148
assertThat(entityType.iterator().hasNext()).isTrue();
148149
assertThat(entityType.iterator().next())
149-
.isEqualTo((TypeInformation) ClassTypeInformation.from(Implementation.class));
150+
.isEqualTo(ClassTypeInformation.from(Implementation.class));
150151
}
151152

152153
@Test // DATAJPA-716

0 commit comments

Comments
 (0)