Skip to content

Commit dc0dcca

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 8b5d090 commit dc0dcca

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

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

Lines changed: 27 additions & 2 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
/*

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)