Skip to content

Commit 16d5ba9

Browse files
committed
Restored binary compatibility with Hibernate 5.0/5.1's Query type
Issue: SPR-14425
1 parent 52065a7 commit 16d5ba9

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

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

+25-5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.springframework.dao.InvalidDataAccessApiUsageException;
4646
import org.springframework.transaction.support.TransactionSynchronizationManager;
4747
import org.springframework.util.Assert;
48+
import org.springframework.util.ReflectionUtils;
4849

4950
/**
5051
* Helper class that simplifies Hibernate data access code. Automatically
@@ -83,6 +84,20 @@
8384
*/
8485
public class HibernateTemplate implements HibernateOperations, InitializingBean {
8586

87+
private static final Method createQueryMethod;
88+
89+
static {
90+
// Hibernate 5.2's createQuery method declares a new subtype as return type,
91+
// so we need to use reflection for binary compatibility with 5.0/5.1 here.
92+
try {
93+
createQueryMethod = Session.class.getMethod("createQuery", String.class);
94+
}
95+
catch (NoSuchMethodException ex) {
96+
throw new IllegalStateException("Incompatible Hibernate Session API", ex);
97+
}
98+
}
99+
100+
86101
protected final Log logger = LogFactory.getLog(getClass());
87102

88103
private SessionFactory sessionFactory;
@@ -863,7 +878,8 @@ public List<?> find(final String queryString, final Object... values) throws Dat
863878
@Override
864879
@SuppressWarnings({"rawtypes", "deprecation"})
865880
public List<?> doInHibernate(Session session) throws HibernateException {
866-
org.hibernate.Query queryObject = session.createQuery(queryString);
881+
org.hibernate.Query queryObject = (org.hibernate.Query)
882+
ReflectionUtils.invokeMethod(createQueryMethod, session, queryString);
867883
prepareQuery(queryObject);
868884
if (values != null) {
869885
for (int i = 0; i < values.length; i++) {
@@ -893,7 +909,8 @@ public List<?> findByNamedParam(final String queryString, final String[] paramNa
893909
@Override
894910
@SuppressWarnings({"rawtypes", "deprecation"})
895911
public List<?> doInHibernate(Session session) throws HibernateException {
896-
org.hibernate.Query queryObject = session.createQuery(queryString);
912+
org.hibernate.Query queryObject = (org.hibernate.Query)
913+
ReflectionUtils.invokeMethod(createQueryMethod, session, queryString);
897914
prepareQuery(queryObject);
898915
for (int i = 0; i < values.length; i++) {
899916
applyNamedParameterToQuery(queryObject, paramNames[i], values[i]);
@@ -911,7 +928,8 @@ public List<?> findByValueBean(final String queryString, final Object valueBean)
911928
@Override
912929
@SuppressWarnings({"rawtypes", "deprecation"})
913930
public List<?> doInHibernate(Session session) throws HibernateException {
914-
org.hibernate.Query queryObject = session.createQuery(queryString);
931+
org.hibernate.Query queryObject = (org.hibernate.Query)
932+
ReflectionUtils.invokeMethod(createQueryMethod, session, queryString);
915933
prepareQuery(queryObject);
916934
queryObject.setProperties(valueBean);
917935
return queryObject.list();
@@ -1072,7 +1090,8 @@ public Iterator<?> iterate(final String queryString, final Object... values) thr
10721090
@Override
10731091
@SuppressWarnings({"rawtypes", "deprecation"})
10741092
public Iterator<?> doInHibernate(Session session) throws HibernateException {
1075-
org.hibernate.Query queryObject = session.createQuery(queryString);
1093+
org.hibernate.Query queryObject = (org.hibernate.Query)
1094+
ReflectionUtils.invokeMethod(createQueryMethod, session, queryString);
10761095
prepareQuery(queryObject);
10771096
if (values != null) {
10781097
for (int i = 0; i < values.length; i++) {
@@ -1100,7 +1119,8 @@ public int bulkUpdate(final String queryString, final Object... values) throws D
11001119
@Override
11011120
@SuppressWarnings({"rawtypes", "deprecation"})
11021121
public Integer doInHibernate(Session session) throws HibernateException {
1103-
org.hibernate.Query queryObject = session.createQuery(queryString);
1122+
org.hibernate.Query queryObject = (org.hibernate.Query)
1123+
ReflectionUtils.invokeMethod(createQueryMethod, session, queryString);
11041124
prepareQuery(queryObject);
11051125
if (values != null) {
11061126
for (int i = 0; i < values.length; i++) {

0 commit comments

Comments
 (0)