Skip to content

Commit 12afc26

Browse files
committed
HibernateTemplate reflectively calls getNamedQuery (for runtime compatibility with Hibernate 5.0/5.1 vs 5.2)
Issue: SPR-14676
1 parent 040d131 commit 12afc26

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

spring-orm-hibernate5/src/main/java/org/springframework/orm/hibernate5/HibernateTemplate.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,14 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean
8787

8888
private static final Method createQueryMethod;
8989

90+
private static final Method getNamedQueryMethod;
91+
9092
static {
9193
// Hibernate 5.2's createQuery method declares a new subtype as return type,
9294
// so we need to use reflection for binary compatibility with 5.0/5.1 here.
9395
try {
9496
createQueryMethod = Session.class.getMethod("createQuery", String.class);
97+
getNamedQueryMethod = Session.class.getMethod("getNamedQuery", String.class);
9598
}
9699
catch (NoSuchMethodException ex) {
97100
throw new IllegalStateException("Incompatible Hibernate Session API", ex);
@@ -955,7 +958,8 @@ public List<?> findByNamedQuery(final String queryName, final Object... values)
955958
@Override
956959
@SuppressWarnings({"rawtypes", "deprecation"})
957960
public List<?> doInHibernate(Session session) throws HibernateException {
958-
org.hibernate.Query queryObject = session.getNamedQuery(queryName);
961+
org.hibernate.Query queryObject = (org.hibernate.Query)
962+
ReflectionUtils.invokeMethod(getNamedQueryMethod, session, queryName);
959963
prepareQuery(queryObject);
960964
if (values != null) {
961965
for (int i = 0; i < values.length; i++) {
@@ -986,7 +990,8 @@ public List<?> findByNamedQueryAndNamedParam(
986990
@Override
987991
@SuppressWarnings({"rawtypes", "deprecation"})
988992
public List<?> doInHibernate(Session session) throws HibernateException {
989-
org.hibernate.Query queryObject = session.getNamedQuery(queryName);
993+
org.hibernate.Query queryObject = (org.hibernate.Query)
994+
ReflectionUtils.invokeMethod(getNamedQueryMethod, session, queryName);
990995
prepareQuery(queryObject);
991996
if (values != null) {
992997
for (int i = 0; i < values.length; i++) {
@@ -1006,7 +1011,8 @@ public List<?> findByNamedQueryAndValueBean(final String queryName, final Object
10061011
@Override
10071012
@SuppressWarnings({"rawtypes", "deprecation"})
10081013
public List<?> doInHibernate(Session session) throws HibernateException {
1009-
org.hibernate.Query queryObject = session.getNamedQuery(queryName);
1014+
org.hibernate.Query queryObject = (org.hibernate.Query)
1015+
ReflectionUtils.invokeMethod(getNamedQueryMethod, session, queryName);
10101016
prepareQuery(queryObject);
10111017
queryObject.setProperties(valueBean);
10121018
return queryObject.list();
@@ -1256,7 +1262,7 @@ public CloseSuppressingInvocationHandler(Session target) {
12561262
}
12571263

12581264
@Override
1259-
@SuppressWarnings("deprecation")
1265+
@SuppressWarnings({"rawtypes", "deprecation"})
12601266
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
12611267
// Invocation on Session interface coming in...
12621268

0 commit comments

Comments
 (0)