DATAJPA-1347 - Added Hibernate-specific ProxyDetector.

This allows us to properly detect Hibernate 5.3 proxies. That version changed the proxy class names being generated in a way that Spring's own ClassUtils.getUserClass(…) doesn't properly return the actual target class anymore. We now build on the infrastructure introduced in DATACMNS-1324 and explicitly check for Hibernate's HibernateProxy interface.

Related tickets: DATACMNS-1324.
This commit is contained in:
Oliver Gierke
2018-05-17 15:10:33 +02:00
parent a8cee96fca
commit 9ce8d958e7
3 changed files with 57 additions and 2 deletions

View File

@@ -23,8 +23,8 @@ import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import org.springframework.data.domain.Persistable;
import org.springframework.data.util.ProxyUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
/**
* Abstract base class for entities. Allows parameterization of id type, chooses auto-generation and implements
@@ -94,7 +94,7 @@ public abstract class AbstractPersistable<PK extends Serializable> implements Pe
return true;
}
if (!getClass().equals(ClassUtils.getUserClass(obj))) {
if (!getClass().equals(ProxyUtils.getUserClass(obj))) {
return false;
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.util;
import java.util.Optional;
import org.hibernate.proxy.HibernateProxy;
import org.springframework.data.util.ProxyUtils.ProxyDetector;
import org.springframework.util.ClassUtils;
/**
* {@link org.springframework.data.util.ProxyDetector} to explicitly check for Hibernate's {@link HibernateProxy}.
*
* @author Oliver Gierke
*/
class HibernateProxyDetector implements ProxyDetector {
private static final Optional<Class<?>> HIBERNATE_PROXY = Optional.ofNullable(loadHibernateProxyType());
/*
* (non-Javadoc)
* @see org.springframework.data.util.ProxyUtils.ProxyDetector#getUserType(java.lang.Class)
*/
@Override
public Class<?> getUserType(Class<?> type) {
return HIBERNATE_PROXY //
.map(it -> it.isAssignableFrom(type) ? type.getSuperclass() : type) //
.filter(it -> !Object.class.equals(it)) //
.orElse(type);
}
private static final Class<?> loadHibernateProxyType() {
try {
return ClassUtils.forName("org.hibernate.proxy.HibernateProxy", HibernateProxyDetector.class.getClassLoader());
} catch (ClassNotFoundException o_O) {
return null;
}
}
}

View File

@@ -1 +1,2 @@
org.springframework.data.repository.core.support.RepositoryFactorySupport=org.springframework.data.jpa.repository.support.JpaRepositoryFactory
org.springframework.data.util.ProxyUtils$ProxyDetector=org.springframework.data.jpa.util.HibernateProxyDetector