Create PersistentEntities only for user class in case of proxy instance.
This commit makes sure to only create a PersistentEntity for an actual user type. Therefore ClassTypeInformation now considers the given type and not only the user one for both equals and hashcode. This makes sure we can distinguish TypeInformation for a proxy from the one of a user class. The AbstractMapping context will take care of unpacking proxied types and registering the created entity for both the user as well as the proxy type information. Prior to this commit build time generated proxy instances would have been able to pollute the context depending on the order they had been served by the initial entity set. Closes #2485 Original pull request: #2486.
This commit is contained in:
committed by
Mark Paluch
parent
69397a11b2
commit
c30f3fc8af
@@ -360,6 +360,17 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
|
||||
return persistentEntity;
|
||||
}
|
||||
|
||||
if(typeInformation.isProxyTypeInformation()) {
|
||||
|
||||
TypeInformation<?> userTypeInformation = typeInformation.getUserTypeInformation();
|
||||
persistentEntity = persistentEntities.get(userTypeInformation);
|
||||
|
||||
if (persistentEntity != null) {
|
||||
persistentEntities.put(typeInformation, persistentEntity);
|
||||
return persistentEntity;
|
||||
}
|
||||
}
|
||||
|
||||
} finally {
|
||||
read.unlock();
|
||||
}
|
||||
@@ -369,7 +380,10 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
|
||||
try {
|
||||
|
||||
write.lock();
|
||||
entity = doAddPersistentEntity(typeInformation);
|
||||
entity = doAddPersistentEntity(typeInformation.getUserTypeInformation());
|
||||
if(typeInformation.isProxyTypeInformation()) {
|
||||
persistentEntities.put(typeInformation, Optional.of(entity));
|
||||
}
|
||||
|
||||
} catch (BeansException e) {
|
||||
throw new MappingException(e.getMessage(), e);
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.springframework.core.GenericTypeResolver;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ConcurrentReferenceHashMap;
|
||||
import org.springframework.util.ConcurrentReferenceHashMap.ReferenceType;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* {@link TypeInformation} for a plain {@link Class}.
|
||||
@@ -177,4 +178,26 @@ public class ClassTypeInformation<S> extends TypeDiscoverer<S> {
|
||||
public String toString() {
|
||||
return type.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!super.equals(o)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ClassTypeInformation<?> that = (ClassTypeInformation<?>) o;
|
||||
return ObjectUtils.nullSafeEquals(type, that.type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
||||
int result = super.hashCode();
|
||||
result = 31 * result + ObjectUtils.nullSafeHashCode(type);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,6 +149,29 @@ public interface TypeInformation<S> {
|
||||
*/
|
||||
Class<S> getType();
|
||||
|
||||
/**
|
||||
* Returns the user type of the property if proxied.
|
||||
*
|
||||
* @return the unpacked (if proxied) type of the property.
|
||||
* @see ProxyUtils#getUserClass(Class)
|
||||
* @since 2.6
|
||||
*/
|
||||
default TypeInformation<?> getUserTypeInformation() {
|
||||
|
||||
Class<?> userType = ProxyUtils.getUserClass(getType());
|
||||
return userType.equals(getType()) ? this : ClassTypeInformation.from(userType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if {@link #getType()} refers to a proxy or user class.
|
||||
*
|
||||
* @return true if type is a proxy.
|
||||
* @since 2.6
|
||||
*/
|
||||
default boolean isProxyTypeInformation() {
|
||||
return !this.equals(getUserTypeInformation());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link ClassTypeInformation} to represent the {@link TypeInformation} of the raw type of the current
|
||||
* instance.
|
||||
|
||||
Reference in New Issue
Block a user