45
45
import org .springframework .dao .InvalidDataAccessApiUsageException ;
46
46
import org .springframework .transaction .support .TransactionSynchronizationManager ;
47
47
import org .springframework .util .Assert ;
48
+ import org .springframework .util .ReflectionUtils ;
48
49
49
50
/**
50
51
* Helper class that simplifies Hibernate data access code. Automatically
83
84
*/
84
85
public class HibernateTemplate implements HibernateOperations , InitializingBean {
85
86
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
+
86
101
protected final Log logger = LogFactory .getLog (getClass ());
87
102
88
103
private SessionFactory sessionFactory ;
@@ -863,7 +878,8 @@ public List<?> find(final String queryString, final Object... values) throws Dat
863
878
@ Override
864
879
@ SuppressWarnings ({"rawtypes" , "deprecation" })
865
880
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 );
867
883
prepareQuery (queryObject );
868
884
if (values != null ) {
869
885
for (int i = 0 ; i < values .length ; i ++) {
@@ -893,7 +909,8 @@ public List<?> findByNamedParam(final String queryString, final String[] paramNa
893
909
@ Override
894
910
@ SuppressWarnings ({"rawtypes" , "deprecation" })
895
911
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 );
897
914
prepareQuery (queryObject );
898
915
for (int i = 0 ; i < values .length ; i ++) {
899
916
applyNamedParameterToQuery (queryObject , paramNames [i ], values [i ]);
@@ -911,7 +928,8 @@ public List<?> findByValueBean(final String queryString, final Object valueBean)
911
928
@ Override
912
929
@ SuppressWarnings ({"rawtypes" , "deprecation" })
913
930
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 );
915
933
prepareQuery (queryObject );
916
934
queryObject .setProperties (valueBean );
917
935
return queryObject .list ();
@@ -1072,7 +1090,8 @@ public Iterator<?> iterate(final String queryString, final Object... values) thr
1072
1090
@ Override
1073
1091
@ SuppressWarnings ({"rawtypes" , "deprecation" })
1074
1092
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 );
1076
1095
prepareQuery (queryObject );
1077
1096
if (values != null ) {
1078
1097
for (int i = 0 ; i < values .length ; i ++) {
@@ -1100,7 +1119,8 @@ public int bulkUpdate(final String queryString, final Object... values) throws D
1100
1119
@ Override
1101
1120
@ SuppressWarnings ({"rawtypes" , "deprecation" })
1102
1121
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 );
1104
1124
prepareQuery (queryObject );
1105
1125
if (values != null ) {
1106
1126
for (int i = 0 ; i < values .length ; i ++) {
0 commit comments