From 9ce8d958e7838cc4cf39f89f1e2584e58813665e Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 17 May 2018 15:10:33 +0200 Subject: [PATCH] DATAJPA-1347 - Added Hibernate-specific ProxyDetector. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../data/jpa/domain/AbstractPersistable.java | 4 +- .../data/jpa/util/HibernateProxyDetector.java | 54 +++++++++++++++++++ src/main/resources/META-INF/spring.factories | 1 + 3 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/springframework/data/jpa/util/HibernateProxyDetector.java diff --git a/src/main/java/org/springframework/data/jpa/domain/AbstractPersistable.java b/src/main/java/org/springframework/data/jpa/domain/AbstractPersistable.java index 47e1736d5..3a51f2e7c 100644 --- a/src/main/java/org/springframework/data/jpa/domain/AbstractPersistable.java +++ b/src/main/java/org/springframework/data/jpa/domain/AbstractPersistable.java @@ -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 implements Pe return true; } - if (!getClass().equals(ClassUtils.getUserClass(obj))) { + if (!getClass().equals(ProxyUtils.getUserClass(obj))) { return false; } diff --git a/src/main/java/org/springframework/data/jpa/util/HibernateProxyDetector.java b/src/main/java/org/springframework/data/jpa/util/HibernateProxyDetector.java new file mode 100644 index 000000000..e48393426 --- /dev/null +++ b/src/main/java/org/springframework/data/jpa/util/HibernateProxyDetector.java @@ -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> 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; + } + } +} diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories index 2418f4a36..cf6372d62 100644 --- a/src/main/resources/META-INF/spring.factories +++ b/src/main/resources/META-INF/spring.factories @@ -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