From 480f9ccffa52b7ad5d9f7caaee98337128f42af9 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 | 36 ++++++++++++++++--- .../AbstractMappingContextUnitTests.java | 2 +- 2 files changed, 32 insertions(+), 6 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 1d9328ac9..70eadc58d 100644 --- a/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java +++ b/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java @@ -218,9 +218,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(); + } } /* @@ -760,7 +785,8 @@ public abstract class AbstractMappingContext persistentEntityForProxy = context .getRequiredPersistentEntity(Base$$SpringProxy$873fa2e.class);