DATAJPA-1359 - Simplified HibernateUtils by removing reference to org.hibernate.Query.

HibernateUtils made heavy use of reflection in order to support different versions of Hibernate. With the current required version of Hibernate as indicated by the pom.xml this degree of flexibility is no longer needed.
This commit is contained in:
Jens Schauder
2018-06-05 14:52:28 +02:00
committed by Oliver Gierke
parent c6f8ee7d2c
commit 91c0424232

View File

@@ -15,14 +15,8 @@
*/
package org.springframework.data.jpa.provider;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.query.Query;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
/**
* Utility functions to work with Hibernate. Mostly using reflection to make sure common functionality can be executed
@@ -30,45 +24,14 @@ import org.springframework.util.ReflectionUtils;
*
* @author Oliver Gierke
* @author Mark Paluch
* @author Jens Schauder
* @since 1.10.2
* @soundtrack Benny Greb - Soulfood (Live, https://www.youtube.com/watch?v=9_ErMa_CtSw)
*/
public abstract class HibernateUtils {
private static final List<String> TYPES = Arrays.asList("org.hibernate.jpa.HibernateQuery",
"org.hibernate.ejb.HibernateQuery");
private static final @Nullable Method GET_HIBERNATE_QUERY;
private static final @Nullable Class<?> HIBERNATE_QUERY_INTERFACE;
private static final @Nullable Method QUERY_STRING_METHOD;
private HibernateUtils() {}
static {
Class<?> type = null;
Method method = null;
ClassLoader classLoader = HibernateUtils.class.getClassLoader();
for (String typeName : TYPES) {
try {
type = ClassUtils.forName(typeName, classLoader);
method = type.getMethod("getHibernateQuery");
} catch (Exception o_O) {}
}
GET_HIBERNATE_QUERY = method;
Class<?> queryInterface = null;
try {
queryInterface = ClassUtils.forName("org.hibernate.query.Query", classLoader);
} catch (Exception o_O) {}
HIBERNATE_QUERY_INTERFACE = queryInterface == null ? type : queryInterface;
QUERY_STRING_METHOD = HIBERNATE_QUERY_INTERFACE == null ? null
: ReflectionUtils.findMethod(HIBERNATE_QUERY_INTERFACE, "getQueryString");
}
/**
* Return the query string of the underlying native Hibernate query.
*
@@ -78,21 +41,10 @@ public abstract class HibernateUtils {
@Nullable
public static String getHibernateQuery(Object query) {
if (HIBERNATE_QUERY_INTERFACE != null && QUERY_STRING_METHOD != null
&& HIBERNATE_QUERY_INTERFACE.isInstance(query)) {
return String.class.cast(ReflectionUtils.invokeMethod(QUERY_STRING_METHOD, query));
if (query instanceof Query) {
return ((Query) query).getQueryString();
} else {
throw new IllegalArgumentException("Don't know how to extract the query string from " + query);
}
if (HIBERNATE_QUERY_INTERFACE != null && !HIBERNATE_QUERY_INTERFACE.isInstance(query)) {
query = ((javax.persistence.Query) query).unwrap(HIBERNATE_QUERY_INTERFACE);
}
if (GET_HIBERNATE_QUERY == null) {
throw new IllegalStateException(
"Cannot invoke getHibernateQuery(…). No underlying method for a reflective call found.");
}
Query q = (Query) ReflectionUtils.invokeMethod(GET_HIBERNATE_QUERY, query);
return q == null ? "" : q.getQueryString();
}
}