Skip to content

Commit 480f9cc

Browse files
committed
Fix PersistentEntity lookup in AbstractMappingContext.hasPersistentEntity(…)
Looking up a PersistentEntity via ….getPersistentEntity(…) applies some massaging of the given type as it could be a proxy type created for a user type. That wrangling was not applied in ….hasPersistentEntity(…) which resulted in a call to that method with a proxy type yielding false although it shouldn't if we already have a PersistentEntity available for the corresponding user type. We now explicitly lookup the entity by original type and, if it's not the user type itself, try to look up the entity for the latter. Fixes #2589.
1 parent 3d2df96 commit 480f9cc

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,34 @@ public boolean hasPersistentEntityFor(Class<?> type) {
218218

219219
Assert.notNull(type, "Type must not be null!");
220220

221-
Optional<E> entity = persistentEntities.get(ClassTypeInformation.from(type));
221+
TypeInformation<?> typeInformation = ClassTypeInformation.from(type);
222222

223-
return entity == null ? false : entity.isPresent();
223+
try {
224+
225+
read.lock();
226+
227+
// Try the original type first
228+
Optional<E> entity = persistentEntities.get(typeInformation);
229+
230+
if (entity != null) {
231+
return entity.isPresent();
232+
}
233+
234+
// User type is the same?
235+
TypeInformation<?> userTypeInformation = typeInformation.getUserTypeInformation();
236+
237+
if (userTypeInformation.equals(typeInformation)) {
238+
return false;
239+
}
240+
241+
// Try the user type
242+
entity = persistentEntities.get(typeInformation.getUserTypeInformation());
243+
244+
return entity == null ? false : entity.isPresent();
245+
246+
} finally {
247+
read.unlock();
248+
}
224249
}
225250

226251
/*
@@ -760,7 +785,8 @@ static class PropertyMatch {
760785
*/
761786
public PropertyMatch(@Nullable String namePattern, @Nullable String typeName) {
762787

763-
Assert.isTrue(!(namePattern == null && typeName == null), "Either name pattern or type name must be given!");
788+
Assert.isTrue(!((namePattern == null) && (typeName == null)),
789+
"Either name pattern or type name must be given!");
764790

765791
this.namePattern = namePattern;
766792
this.typeName = typeName;
@@ -778,11 +804,11 @@ public boolean matches(String name, Class<?> type) {
778804
Assert.notNull(name, "Name must not be null!");
779805
Assert.notNull(type, "Type must not be null!");
780806

781-
if (namePattern != null && !name.matches(namePattern)) {
807+
if ((namePattern != null) && !name.matches(namePattern)) {
782808
return false;
783809
}
784810

785-
if (typeName != null && !type.getName().equals(typeName)) {
811+
if ((typeName != null) && !type.getName().equals(typeName)) {
786812
return false;
787813
}
788814

src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ void contextSeesUserTypeBeforeProxy() {
334334
persistentEntity.getTypeInformation().getType().equals(Base.class);
335335

336336
assertThat(context.hasPersistentEntityFor(Base.class)).isTrue();
337-
assertThat(context.hasPersistentEntityFor(Base$$SpringProxy$873fa2e.class)).isFalse();
337+
assertThat(context.hasPersistentEntityFor(Base$$SpringProxy$873fa2e.class)).isTrue();
338338

339339
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntityForProxy = context
340340
.getRequiredPersistentEntity(Base$$SpringProxy$873fa2e.class);

0 commit comments

Comments
 (0)