Skip to content

Commit d631b4c

Browse files
committed
HibernateTemplate reflectively calls getNamedQuery (for runtime compatibility with Hibernate 5.0/5.1 vs 5.2)
Issue: SPR-14676
1 parent 8c56606 commit d631b4c

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

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

Lines changed: 9 additions & 3 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();

0 commit comments

Comments
 (0)