提交 19e5a34f 编写于 作者: J Juergen Hoeller

Restored binary compatibility with Hibernate 5.0/5.1's Query type

Issue: SPR-14425
上级 5890758f
...@@ -45,6 +45,7 @@ import org.springframework.dao.DataAccessException; ...@@ -45,6 +45,7 @@ import org.springframework.dao.DataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
/** /**
* Helper class that simplifies Hibernate data access code. Automatically * Helper class that simplifies Hibernate data access code. Automatically
...@@ -83,6 +84,20 @@ import org.springframework.util.Assert; ...@@ -83,6 +84,20 @@ import org.springframework.util.Assert;
*/ */
public class HibernateTemplate implements HibernateOperations, InitializingBean { public class HibernateTemplate implements HibernateOperations, InitializingBean {
private static final Method createQueryMethod;
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);
}
catch (NoSuchMethodException ex) {
throw new IllegalStateException("Incompatible Hibernate Session API", ex);
}
}
protected final Log logger = LogFactory.getLog(getClass()); protected final Log logger = LogFactory.getLog(getClass());
private SessionFactory sessionFactory; private SessionFactory sessionFactory;
...@@ -863,7 +878,8 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean ...@@ -863,7 +878,8 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean
@Override @Override
@SuppressWarnings({"rawtypes", "deprecation"}) @SuppressWarnings({"rawtypes", "deprecation"})
public List<?> doInHibernate(Session session) throws HibernateException { public List<?> doInHibernate(Session session) throws HibernateException {
org.hibernate.Query queryObject = session.createQuery(queryString); org.hibernate.Query queryObject = (org.hibernate.Query)
ReflectionUtils.invokeMethod(createQueryMethod, session, queryString);
prepareQuery(queryObject); prepareQuery(queryObject);
if (values != null) { if (values != null) {
for (int i = 0; i < values.length; i++) { for (int i = 0; i < values.length; i++) {
...@@ -893,7 +909,8 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean ...@@ -893,7 +909,8 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean
@Override @Override
@SuppressWarnings({"rawtypes", "deprecation"}) @SuppressWarnings({"rawtypes", "deprecation"})
public List<?> doInHibernate(Session session) throws HibernateException { public List<?> doInHibernate(Session session) throws HibernateException {
org.hibernate.Query queryObject = session.createQuery(queryString); org.hibernate.Query queryObject = (org.hibernate.Query)
ReflectionUtils.invokeMethod(createQueryMethod, session, queryString);
prepareQuery(queryObject); prepareQuery(queryObject);
for (int i = 0; i < values.length; i++) { for (int i = 0; i < values.length; i++) {
applyNamedParameterToQuery(queryObject, paramNames[i], values[i]); applyNamedParameterToQuery(queryObject, paramNames[i], values[i]);
...@@ -911,7 +928,8 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean ...@@ -911,7 +928,8 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean
@Override @Override
@SuppressWarnings({"rawtypes", "deprecation"}) @SuppressWarnings({"rawtypes", "deprecation"})
public List<?> doInHibernate(Session session) throws HibernateException { public List<?> doInHibernate(Session session) throws HibernateException {
org.hibernate.Query queryObject = session.createQuery(queryString); org.hibernate.Query queryObject = (org.hibernate.Query)
ReflectionUtils.invokeMethod(createQueryMethod, session, queryString);
prepareQuery(queryObject); prepareQuery(queryObject);
queryObject.setProperties(valueBean); queryObject.setProperties(valueBean);
return queryObject.list(); return queryObject.list();
...@@ -1072,7 +1090,8 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean ...@@ -1072,7 +1090,8 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean
@Override @Override
@SuppressWarnings({"rawtypes", "deprecation"}) @SuppressWarnings({"rawtypes", "deprecation"})
public Iterator<?> doInHibernate(Session session) throws HibernateException { public Iterator<?> doInHibernate(Session session) throws HibernateException {
org.hibernate.Query queryObject = session.createQuery(queryString); org.hibernate.Query queryObject = (org.hibernate.Query)
ReflectionUtils.invokeMethod(createQueryMethod, session, queryString);
prepareQuery(queryObject); prepareQuery(queryObject);
if (values != null) { if (values != null) {
for (int i = 0; i < values.length; i++) { for (int i = 0; i < values.length; i++) {
...@@ -1100,7 +1119,8 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean ...@@ -1100,7 +1119,8 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean
@Override @Override
@SuppressWarnings({"rawtypes", "deprecation"}) @SuppressWarnings({"rawtypes", "deprecation"})
public Integer doInHibernate(Session session) throws HibernateException { public Integer doInHibernate(Session session) throws HibernateException {
org.hibernate.Query queryObject = session.createQuery(queryString); org.hibernate.Query queryObject = (org.hibernate.Query)
ReflectionUtils.invokeMethod(createQueryMethod, session, queryString);
prepareQuery(queryObject); prepareQuery(queryObject);
if (values != null) { if (values != null) {
for (int i = 0; i < values.length; i++) { for (int i = 0; i < values.length; i++) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册