From 12afc263a46b39886d7ff9918813a3dafd02a983 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 13 Sep 2016 22:32:36 +0200 Subject: [PATCH] HibernateTemplate reflectively calls getNamedQuery (for runtime compatibility with Hibernate 5.0/5.1 vs 5.2) Issue: SPR-14676 --- .../orm/hibernate5/HibernateTemplate.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/spring-orm-hibernate5/src/main/java/org/springframework/orm/hibernate5/HibernateTemplate.java b/spring-orm-hibernate5/src/main/java/org/springframework/orm/hibernate5/HibernateTemplate.java index 83b43d4af36..b671231f7f8 100644 --- a/spring-orm-hibernate5/src/main/java/org/springframework/orm/hibernate5/HibernateTemplate.java +++ b/spring-orm-hibernate5/src/main/java/org/springframework/orm/hibernate5/HibernateTemplate.java @@ -87,11 +87,14 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean private static final Method createQueryMethod; + private static final Method getNamedQueryMethod; + static { // Hibernate 5.2's createQuery method declares a new subtype as return type, // so we need to use reflection for binary compatibility with 5.0/5.1 here. try { createQueryMethod = Session.class.getMethod("createQuery", String.class); + getNamedQueryMethod = Session.class.getMethod("getNamedQuery", String.class); } catch (NoSuchMethodException ex) { throw new IllegalStateException("Incompatible Hibernate Session API", ex); @@ -955,7 +958,8 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean @Override @SuppressWarnings({"rawtypes", "deprecation"}) public List doInHibernate(Session session) throws HibernateException { - org.hibernate.Query queryObject = session.getNamedQuery(queryName); + org.hibernate.Query queryObject = (org.hibernate.Query) + ReflectionUtils.invokeMethod(getNamedQueryMethod, session, queryName); prepareQuery(queryObject); if (values != null) { for (int i = 0; i < values.length; i++) { @@ -986,7 +990,8 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean @Override @SuppressWarnings({"rawtypes", "deprecation"}) public List doInHibernate(Session session) throws HibernateException { - org.hibernate.Query queryObject = session.getNamedQuery(queryName); + org.hibernate.Query queryObject = (org.hibernate.Query) + ReflectionUtils.invokeMethod(getNamedQueryMethod, session, queryName); prepareQuery(queryObject); if (values != null) { for (int i = 0; i < values.length; i++) { @@ -1006,7 +1011,8 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean @Override @SuppressWarnings({"rawtypes", "deprecation"}) public List doInHibernate(Session session) throws HibernateException { - org.hibernate.Query queryObject = session.getNamedQuery(queryName); + org.hibernate.Query queryObject = (org.hibernate.Query) + ReflectionUtils.invokeMethod(getNamedQueryMethod, session, queryName); prepareQuery(queryObject); queryObject.setProperties(valueBean); return queryObject.list(); @@ -1256,7 +1262,7 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean } @Override - @SuppressWarnings("deprecation") + @SuppressWarnings({"rawtypes", "deprecation"}) public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // Invocation on Session interface coming in...