41
41
import org.hibernate.SessionFactory;
42
42
import org.hibernate.criterion.DetachedCriteria;
43
43
import org.hibernate.criterion.Example;
44
+ import org.hibernate.query.Query;
44
45
45
46
import org.springframework.beans.factory.InitializingBean;
46
47
import org.springframework.dao.DataAccessException;
49
50
import org.springframework.transaction.support.ResourceHolderSupport;
50
51
import org.springframework.transaction.support.TransactionSynchronizationManager;
51
52
import org.springframework.util.Assert;
52
- import org.springframework.util.ReflectionUtils;
53
53
54
54
/**
55
55
* Helper class that simplifies Hibernate data access code. Automatically
90
90
*/
91
91
public class HibernateTemplate implements HibernateOperations, InitializingBean {
92
92
93
- private static final Method createQueryMethod;
94
-
95
- private static final Method getNamedQueryMethod;
96
-
97
- static {
98
- // Hibernate 5.2's createQuery method declares a new subtype as return type,
99
- // so we need to use reflection for binary compatibility with 5.0/5.1 here.
100
- try {
101
- createQueryMethod = Session.class.getMethod("createQuery", String.class);
102
- getNamedQueryMethod = Session.class.getMethod("getNamedQuery", String.class);
103
- }
104
- catch (NoSuchMethodException ex) {
105
- throw new IllegalStateException("Incompatible Hibernate Session API", ex);
106
- }
107
- }
108
-
109
-
110
93
protected final Log logger = LogFactory.getLog(getClass());
111
94
112
95
@Nullable
@@ -250,7 +233,7 @@ public boolean isCheckWriteOperations() {
250
233
* <p>To specify the query region to be used for queries cached
251
234
* by this template, set the "queryCacheRegion" property.
252
235
* @see #setQueryCacheRegion
253
- * @see org.hibernate. Query#setCacheable
236
+ * @see Query#setCacheable
254
237
* @see Criteria#setCacheable
255
238
*/
256
239
public void setCacheQueries(boolean cacheQueries) {
@@ -271,7 +254,7 @@ public boolean isCacheQueries() {
271
254
* <p>The cache region will not take effect unless queries created by this
272
255
* template are configured to be cached via the "cacheQueries" property.
273
256
* @see #setCacheQueries
274
- * @see org.hibernate. Query#setCacheRegion
257
+ * @see Query#setCacheRegion
275
258
* @see Criteria#setCacheRegion
276
259
*/
277
260
public void setQueryCacheRegion(@Nullable String queryCacheRegion) {
@@ -359,7 +342,6 @@ public <T> T executeWithNativeSession(HibernateCallback<T> action) {
359
342
* @return a result object returned by the action, or {@code null}
360
343
* @throws DataAccessException in case of Hibernate errors
361
344
*/
362
- @SuppressWarnings("deprecation")
363
345
@Nullable
364
346
protected <T> T doExecute(HibernateCallback<T> action, boolean enforceNativeSession) throws DataAccessException {
365
347
Assert.notNull(action, "Callback object must not be null");
@@ -374,7 +356,7 @@ protected <T> T doExecute(HibernateCallback<T> action, boolean enforceNativeSess
374
356
}
375
357
if (session == null) {
376
358
session = obtainSessionFactory().openSession();
377
- session.setFlushMode (FlushMode.MANUAL);
359
+ session.setHibernateFlushMode (FlushMode.MANUAL);
378
360
isNew = true;
379
361
}
380
362
@@ -543,7 +525,6 @@ public <T> List<T> loadAll(Class<T> entityClass) throws DataAccessException {
543
525
}
544
526
545
527
@Override
546
- @SuppressWarnings({"deprecation"})
547
528
public void load(Object entity, Serializable id) throws DataAccessException {
548
529
executeWithNativeSession(session -> {
549
530
session.load(entity, id);
@@ -887,11 +868,9 @@ public <T> List<T> findByExample(@Nullable String entityName, T exampleEntity, i
887
868
888
869
@Deprecated
889
870
@Override
890
- @SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
891
871
public List<?> find(String queryString, @Nullable Object... values) throws DataAccessException {
892
872
return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
893
- org.hibernate.Query queryObject = queryObject(
894
- ReflectionUtils.invokeMethod(createQueryMethod, session, queryString));
873
+ Query<?> queryObject = session.createQuery(queryString);
895
874
prepareQuery(queryObject);
896
875
if (values != null) {
897
876
for (int i = 0; i < values.length; i++) {
@@ -912,16 +891,14 @@ public List<?> findByNamedParam(String queryString, String paramName, Object val
912
891
913
892
@Deprecated
914
893
@Override
915
- @SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
916
894
public List<?> findByNamedParam(String queryString, String[] paramNames, Object[] values)
917
895
throws DataAccessException {
918
896
919
897
if (paramNames.length != values.length) {
920
898
throw new IllegalArgumentException("Length of paramNames array must match length of values array");
921
899
}
922
900
return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
923
- org.hibernate.Query queryObject = queryObject(
924
- ReflectionUtils.invokeMethod(createQueryMethod, session, queryString));
901
+ Query<?> queryObject = session.createQuery(queryString);
925
902
prepareQuery(queryObject);
926
903
for (int i = 0; i < values.length; i++) {
927
904
applyNamedParameterToQuery(queryObject, paramNames[i], values[i]);
@@ -932,12 +909,9 @@ public List<?> findByNamedParam(String queryString, String[] paramNames, Object[
932
909
933
910
@Deprecated
934
911
@Override
935
- @SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
936
912
public List<?> findByValueBean(String queryString, Object valueBean) throws DataAccessException {
937
-
938
913
return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
939
- org.hibernate.Query queryObject = queryObject(
940
- ReflectionUtils.invokeMethod(createQueryMethod, session, queryString));
914
+ Query<?> queryObject = session.createQuery(queryString);
941
915
prepareQuery(queryObject);
942
916
queryObject.setProperties(valueBean);
943
917
return queryObject.list();
@@ -951,11 +925,9 @@ public List<?> findByValueBean(String queryString, Object valueBean) throws Data
951
925
952
926
@Deprecated
953
927
@Override
954
- @SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
955
928
public List<?> findByNamedQuery(String queryName, @Nullable Object... values) throws DataAccessException {
956
929
return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
957
- org.hibernate.Query queryObject = queryObject(
958
- ReflectionUtils.invokeMethod(getNamedQueryMethod, session, queryName));
930
+ Query<?> queryObject = session.getNamedQuery(queryName);
959
931
prepareQuery(queryObject);
960
932
if (values != null) {
961
933
for (int i = 0; i < values.length; i++) {
@@ -976,7 +948,6 @@ public List<?> findByNamedQueryAndNamedParam(String queryName, String paramName,
976
948
977
949
@Deprecated
978
950
@Override
979
- @SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
980
951
public List<?> findByNamedQueryAndNamedParam(
981
952
String queryName, @Nullable String[] paramNames, @Nullable Object[] values)
982
953
throws DataAccessException {
@@ -985,8 +956,7 @@ public List<?> findByNamedQueryAndNamedParam(
985
956
throw new IllegalArgumentException("Length of paramNames array must match length of values array");
986
957
}
987
958
return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
988
- org.hibernate.Query queryObject = (org.hibernate.Query)
989
- nonNull(ReflectionUtils.invokeMethod(getNamedQueryMethod, session, queryName));
959
+ Query<?> queryObject = session.getNamedQuery(queryName);
990
960
prepareQuery(queryObject);
991
961
if (values != null) {
992
962
for (int i = 0; i < values.length; i++) {
@@ -999,12 +969,9 @@ public List<?> findByNamedQueryAndNamedParam(
999
969
1000
970
@Deprecated
1001
971
@Override
1002
- @SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
1003
972
public List<?> findByNamedQueryAndValueBean(String queryName, Object valueBean) throws DataAccessException {
1004
-
1005
973
return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
1006
- org.hibernate.Query queryObject = queryObject(
1007
- ReflectionUtils.invokeMethod(getNamedQueryMethod, session, queryName));
974
+ Query<?> queryObject = session.getNamedQuery(queryName);
1008
975
prepareQuery(queryObject);
1009
976
queryObject.setProperties(valueBean);
1010
977
return queryObject.list();
@@ -1018,11 +985,9 @@ public List<?> findByNamedQueryAndValueBean(String queryName, Object valueBean)
1018
985
1019
986
@Deprecated
1020
987
@Override
1021
- @SuppressWarnings({"rawtypes", "deprecation"})
1022
988
public Iterator<?> iterate(String queryString, @Nullable Object... values) throws DataAccessException {
1023
989
return nonNull(executeWithNativeSession((HibernateCallback<Iterator<?>>) session -> {
1024
- org.hibernate.Query queryObject = queryObject(
1025
- ReflectionUtils.invokeMethod(createQueryMethod, session, queryString));
990
+ Query<?> queryObject = session.createQuery(queryString);
1026
991
prepareQuery(queryObject);
1027
992
if (values != null) {
1028
993
for (int i = 0; i < values.length; i++) {
@@ -1046,11 +1011,9 @@ public void closeIterator(Iterator<?> it) throws DataAccessException {
1046
1011
1047
1012
@Deprecated
1048
1013
@Override
1049
- @SuppressWarnings({"rawtypes", "deprecation"})
1050
1014
public int bulkUpdate(String queryString, @Nullable Object... values) throws DataAccessException {
1051
1015
Integer result = executeWithNativeSession(session -> {
1052
- org.hibernate.Query queryObject = queryObject(
1053
- ReflectionUtils.invokeMethod(createQueryMethod, session, queryString));
1016
+ Query<?> queryObject = session.createQuery(queryString);
1054
1017
prepareQuery(queryObject);
1055
1018
if (values != null) {
1056
1019
for (int i = 0; i < values.length; i++) {
@@ -1079,7 +1042,7 @@ public int bulkUpdate(String queryString, @Nullable Object... values) throws Dat
1079
1042
* @see FlushMode#MANUAL
1080
1043
*/
1081
1044
protected void checkWriteOperationAllowed(Session session) throws InvalidDataAccessApiUsageException {
1082
- if (isCheckWriteOperations() && SessionFactoryUtils.getFlushMode(session ).lessThan(FlushMode.COMMIT)) {
1045
+ if (isCheckWriteOperations() && session.getHibernateFlushMode( ).lessThan(FlushMode.COMMIT)) {
1083
1046
throw new InvalidDataAccessApiUsageException(
1084
1047
"Write operations are not allowed in read-only mode (FlushMode.MANUAL): "+
1085
1048
"Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.");
@@ -1121,8 +1084,7 @@ protected void prepareCriteria(Criteria criteria) {
1121
1084
* @see #setCacheQueries
1122
1085
* @see #setQueryCacheRegion
1123
1086
*/
1124
- @SuppressWarnings({"rawtypes", "deprecation"})
1125
- protected void prepareQuery(org.hibernate.Query queryObject) {
1087
+ protected void prepareQuery(Query<?> queryObject) {
1126
1088
if (isCacheQueries()) {
1127
1089
queryObject.setCacheable(true);
1128
1090
if (getQueryCacheRegion() != null) {
@@ -1150,9 +1112,7 @@ protected void prepareQuery(org.hibernate.Query queryObject) {
1150
1112
* @param value the value of the parameter
1151
1113
* @throws HibernateException if thrown by the Query object
1152
1114
*/
1153
- @Deprecated
1154
- @SuppressWarnings({"rawtypes", "deprecation"})
1155
- protected void applyNamedParameterToQuery(org.hibernate.Query queryObject, String paramName, Object value)
1115
+ protected void applyNamedParameterToQuery(Query<?> queryObject, String paramName, Object value)
1156
1116
throws HibernateException {
1157
1117
1158
1118
if (value instanceof Collection) {
@@ -1166,13 +1126,6 @@ else if (value instanceof Object[]) {
1166
1126
}
1167
1127
}
1168
1128
1169
- @Deprecated
1170
- @SuppressWarnings({"rawtypes", "deprecation"})
1171
- private static org.hibernate.Query queryObject(@Nullable Object result) {
1172
- Assert.state(result != null, "No Hibernate Query");
1173
- return (org.hibernate.Query) result;
1174
- }
1175
-
1176
1129
private static <T> T nonNull(@Nullable T result) {
1177
1130
Assert.state(result != null, "No result");
1178
1131
return result;
@@ -1193,7 +1146,6 @@ public CloseSuppressingInvocationHandler(Session target) {
1193
1146
}
1194
1147
1195
1148
@Override
1196
- @SuppressWarnings({"rawtypes", "deprecation"})
1197
1149
@Nullable
1198
1150
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
1199
1151
// Invocation on Session interface coming in...
@@ -1219,8 +1171,8 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
1219
1171
if (retVal instanceof Criteria) {
1220
1172
prepareCriteria(((Criteria) retVal));
1221
1173
}
1222
- else if (retVal instanceof org.hibernate. Query) {
1223
- prepareQuery(((org.hibernate. Query) retVal));
1174
+ else if (retVal instanceof Query) {
1175
+ prepareQuery(((Query<?> ) retVal));
1224
1176
}
1225
1177
1226
1178
return retVal;
0 commit comments