From dc0dcca16d5461bf5a0feb79f18df8a57c1cb510 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 | 2 +- 2 files changed, 28 insertions(+), 3 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..4fd3a3562 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(); + } } /* 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 53cb696a5..3464a28b7 100755 --- a/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java @@ -334,7 +334,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);