Skip to content

Commit c643d2d

Browse files
committed
AbstractPersistentProperty now considers association target type for ….getPersistentEntityTypes().
Prior to this commit, the lookup of types to be added as persistent entities for a given PersistentProperty used the actual property type which did not consider the association target type at all. Removed the usage of Optional for the entity type information and switched to use TypeInformation for the raw association target type so that it can be considered in ….getActualType().
1 parent 5f36ebf commit c643d2d

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

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

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Collections;
2222
import java.util.Map;
2323
import java.util.Optional;
24+
import java.util.function.Supplier;
2425

2526
import org.springframework.data.mapping.Association;
2627
import org.springframework.data.mapping.PersistentEntity;
@@ -56,15 +57,13 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>
5657
private final Class<?> rawType;
5758
private final Lazy<Association<P>> association;
5859
private final PersistentEntity<?, P> owner;
59-
60-
@SuppressWarnings("null") //
6160
private final Property property;
6261
private final Lazy<Integer> hashCode;
6362
private final Lazy<Boolean> usePropertyAccess;
64-
private final Lazy<Optional<? extends TypeInformation<?>>> entityTypeInformation;
63+
private final Lazy<TypeInformation<?>> entityTypeInformation;
6564

6665
private final Lazy<Boolean> isAssociation;
67-
private final Lazy<Class<?>> associationTargetType;
66+
private final Lazy<TypeInformation<?>> associationTargetType;
6867

6968
private final Method getter;
7069
private final Method setter;
@@ -94,13 +93,13 @@ public AbstractPersistentProperty(Property property, PersistentEntity<?, P> owne
9493
: Lazy.of(() -> Optional.of(getTypeInformation())
9594
.map(it -> it.getSuperTypeInformation(ASSOCIATION_TYPE))
9695
.map(TypeInformation::getComponentType)
97-
.map(TypeInformation::getType)
9896
.orElse(null));
9997

100-
this.entityTypeInformation = Lazy.of(() -> Optional.ofNullable(information.getActualType())//
101-
.filter(it -> !simpleTypeHolder.isSimpleType(it.getType()))//
102-
.filter(it -> !it.isCollectionLike())//
103-
.filter(it -> !it.isMap()));
98+
this.entityTypeInformation = Lazy.of(() -> Optional.ofNullable(getAssociationOrActualType())
99+
.filter(it -> !simpleTypeHolder.isSimpleType(it.getType())) //
100+
.filter(it -> !it.isCollectionLike()) //
101+
.filter(it -> !it.isMap())
102+
.orElse(null));
104103

105104
this.getter = property.getGetter().orElse(null);
106105
this.setter = property.getSetter().orElse(null);
@@ -172,9 +171,9 @@ public Iterable<? extends TypeInformation<?>> getPersistentEntityTypes() {
172171
return Collections.emptySet();
173172
}
174173

175-
return entityTypeInformation.get()//
176-
.map(Collections::singleton)//
177-
.orElseGet(Collections::emptySet);
174+
TypeInformation<?> result = getAssociationTypeOr(() -> entityTypeInformation.getNullable());
175+
176+
return result != null ? Collections.singleton(result) : Collections.emptySet();
178177
}
179178

180179
/*
@@ -279,7 +278,10 @@ public Association<P> getAssociation() {
279278
@Nullable
280279
@Override
281280
public Class<?> getAssociationTargetType() {
282-
return associationTargetType.getNullable();
281+
282+
TypeInformation<?> result = associationTargetType.getNullable();
283+
284+
return result != null ? result.getType() : null;
283285
}
284286

285287
/*
@@ -315,7 +317,7 @@ public boolean isArray() {
315317
*/
316318
@Override
317319
public boolean isEntity() {
318-
return !isTransient() && entityTypeInformation.get().isPresent();
320+
return !isTransient() && entityTypeInformation.getNullable() != null;
319321
}
320322

321323
/*
@@ -339,6 +341,7 @@ public Class<?> getMapValueType() {
339341
if (isMap()) {
340342

341343
TypeInformation<?> mapValueType = information.getMapValueType();
344+
342345
if (mapValueType != null) {
343346
return mapValueType.getType();
344347
}
@@ -353,7 +356,7 @@ public Class<?> getMapValueType() {
353356
*/
354357
@Override
355358
public Class<?> getActualType() {
356-
return information.getRequiredActualType().getType();
359+
return getRequiredAssociationOrActualType().getType();
357360
}
358361

359362
/*
@@ -364,7 +367,6 @@ public boolean usePropertyAccess() {
364367
return usePropertyAccess.get();
365368
}
366369

367-
@SuppressWarnings("null")
368370
protected Property getProperty() {
369371
return this.property;
370372
}
@@ -406,4 +408,24 @@ public int hashCode() {
406408
public String toString() {
407409
return property.toString();
408410
}
411+
412+
@Nullable
413+
private TypeInformation<?> getAssociationOrActualType() {
414+
return getAssociationTypeOr(() -> information.getActualType());
415+
}
416+
417+
private TypeInformation<?> getRequiredAssociationOrActualType() {
418+
return getAssociationTypeOr(() -> information.getRequiredActualType());
419+
}
420+
421+
private TypeInformation<?> getAssociationTypeOr(Supplier<TypeInformation<?>> fallback) {
422+
423+
TypeInformation<?> result = associationTargetType.getNullable();
424+
425+
if (result != null) {
426+
return result;
427+
}
428+
429+
return fallback.get();
430+
}
409431
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ void detectsJMoleculesAssociation() {
233233

234234
assertThat(property.isAssociation()).isTrue();
235235
assertThat(property.getAssociationTargetType()).isEqualTo(JMoleculesAggregate.class);
236+
assertThat(property.getPersistentEntityTypes())
237+
.extracting(it -> it.getType())
238+
.containsExactly((Class) JMoleculesAggregate.class);
236239
}
237240

238241
private <T> BasicPersistentEntity<T, SamplePersistentProperty> getEntity(Class<T> type) {

0 commit comments

Comments
 (0)