Browse Source

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.
pull/2616/head
Oliver Drotbohm 4 years ago
parent
commit
dc0dcca16d
No known key found for this signature in database
GPG Key ID: C25FBFA0DA493A1D
  1. 29
      src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java
  2. 2
      src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java

29
src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java

@ -218,9 +218,34 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<? @@ -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();
}
}
/*

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

@ -334,7 +334,7 @@ class AbstractMappingContextUnitTests { @@ -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);

Loading…
Cancel
Save