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.
This commit is contained in:
Oliver Drotbohm
2022-04-08 11:35:07 +02:00
parent 3d2df962a8
commit 480f9ccffa
2 changed files with 32 additions and 6 deletions

View File

@@ -218,9 +218,34 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
Assert.notNull(type, "Type must not be null!");
Optional<E> 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<E> 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<E extends MutablePersistentEntity<?
*/
public PropertyMatch(@Nullable String namePattern, @Nullable String typeName) {
Assert.isTrue(!(namePattern == null && typeName == null), "Either name pattern or type name must be given!");
Assert.isTrue(!((namePattern == null) && (typeName == null)),
"Either name pattern or type name must be given!");
this.namePattern = namePattern;
this.typeName = typeName;
@@ -778,11 +804,11 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
Assert.notNull(name, "Name must not be null!");
Assert.notNull(type, "Type must not be null!");
if (namePattern != null && !name.matches(namePattern)) {
if ((namePattern != null) && !name.matches(namePattern)) {
return false;
}
if (typeName != null && !type.getName().equals(typeName)) {
if ((typeName != null) && !type.getName().equals(typeName)) {
return false;
}

View File

@@ -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<Object, SamplePersistentProperty> persistentEntityForProxy = context
.getRequiredPersistentEntity(Base$$SpringProxy$873fa2e.class);