From 1792cd8f452cac9376d80e72892c8a6fcd6574ec Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Fri, 8 Apr 2022 11:35:07 +0200 Subject: [PATCH] =?UTF-8?q?Fix=20PersistentEntity=20lookup=20in=20Abstract?= =?UTF-8?q?MappingContext.hasPersistentEntity(=E2=80=A6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../context/AbstractMappingContext.java | 29 +++++++++++++++++-- .../AbstractMappingContextUnitTests.java | 3 +- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java b/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java index deeec8c62..243287124 100644 --- a/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java +++ b/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java @@ -197,9 +197,34 @@ public abstract class AbstractMappingContext entity = persistentEntities.get(ClassTypeInformation.from(type)); + TypeInformation typeInformation = ClassTypeInformation.from(type); - return entity == null ? false : entity.isPresent(); + try { + + read.lock(); + + // Try the original type first + Optional entity = persistentEntities.get(typeInformation); + + if (entity != null) { + return entity.isPresent(); + } + + // User type is the same? + TypeInformation userTypeInformation = typeInformation.getUserTypeInformation(); + + if (userTypeInformation.equals(typeInformation)) { + return false; + } + + // Try the user type + entity = persistentEntities.get(typeInformation.getUserTypeInformation()); + + return entity == null ? false : entity.isPresent(); + + } finally { + read.unlock(); + } } @Nullable diff --git a/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java b/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java index 969b708d6..fe1cff15b 100755 --- a/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java @@ -40,7 +40,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.aop.SpringProxy; import org.springframework.aop.framework.Advised; - import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationEvent; import org.springframework.data.annotation.Id; @@ -334,7 +333,7 @@ class AbstractMappingContextUnitTests { persistentEntity.getTypeInformation().getType().equals(Base.class); assertThat(context.hasPersistentEntityFor(Base.class)).isTrue(); - assertThat(context.hasPersistentEntityFor(Base$$SpringProxy$873fa2e.class)).isFalse(); + assertThat(context.hasPersistentEntityFor(Base$$SpringProxy$873fa2e.class)).isTrue(); BasicPersistentEntity persistentEntityForProxy = context .getRequiredPersistentEntity(Base$$SpringProxy$873fa2e.class);