Skip to content

Commit 1792cd8

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 e25fb0b commit 1792cd8

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
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
@@ -197,9 +197,34 @@ public boolean hasPersistentEntityFor(Class<?> type) {
197197

198198
Assert.notNull(type, "Type must not be null!");
199199

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

202-
return entity == null ? false : entity.isPresent();
202+
try {
203+
204+
read.lock();
205+
206+
// Try the original type first
207+
Optional<E> entity = persistentEntities.get(typeInformation);
208+
209+
if (entity != null) {
210+
return entity.isPresent();
211+
}
212+
213+
// User type is the same?
214+
TypeInformation<?> userTypeInformation = typeInformation.getUserTypeInformation();
215+
216+
if (userTypeInformation.equals(typeInformation)) {
217+
return false;
218+
}
219+
220+
// Try the user type
221+
entity = persistentEntities.get(typeInformation.getUserTypeInformation());
222+
223+
return entity == null ? false : entity.isPresent();
224+
225+
} finally {
226+
read.unlock();
227+
}
203228
}
204229

205230
@Nullable

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.junit.jupiter.api.Test;
4141
import org.springframework.aop.SpringProxy;
4242
import org.springframework.aop.framework.Advised;
43-
4443
import org.springframework.context.ApplicationContext;
4544
import org.springframework.context.ApplicationEvent;
4645
import org.springframework.data.annotation.Id;
@@ -334,7 +333,7 @@ void contextSeesUserTypeBeforeProxy() {
334333
persistentEntity.getTypeInformation().getType().equals(Base.class);
335334

336335
assertThat(context.hasPersistentEntityFor(Base.class)).isTrue();
337-
assertThat(context.hasPersistentEntityFor(Base$$SpringProxy$873fa2e.class)).isFalse();
336+
assertThat(context.hasPersistentEntityFor(Base$$SpringProxy$873fa2e.class)).isTrue();
338337

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

0 commit comments

Comments
 (0)