4141import org .hibernate .SessionFactory ;
4242import org .hibernate .criterion .DetachedCriteria ;
4343import org .hibernate .criterion .Example ;
44+ import org .hibernate .query .Query ;
4445
4546import org .springframework .beans .factory .InitializingBean ;
4647import org .springframework .dao .DataAccessException ;
4950import org .springframework .transaction .support .ResourceHolderSupport ;
5051import org .springframework .transaction .support .TransactionSynchronizationManager ;
5152import org .springframework .util .Assert ;
52- import org .springframework .util .ReflectionUtils ;
5353
5454/**
5555 * Helper class that simplifies Hibernate data access code. Automatically
9090 */
9191public class HibernateTemplate implements HibernateOperations , InitializingBean {
9292
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-
11093 protected final Log logger = LogFactory .getLog (getClass ());
11194
11295 @ Nullable
@@ -250,7 +233,7 @@ public boolean isCheckWriteOperations() {
250233 * <p>To specify the query region to be used for queries cached
251234 * by this template, set the "queryCacheRegion" property.
252235 * @see #setQueryCacheRegion
253- * @see org.hibernate. Query#setCacheable
236+ * @see Query#setCacheable
254237 * @see Criteria#setCacheable
255238 */
256239 public void setCacheQueries (boolean cacheQueries ) {
@@ -271,7 +254,7 @@ public boolean isCacheQueries() {
271254 * <p>The cache region will not take effect unless queries created by this
272255 * template are configured to be cached via the "cacheQueries" property.
273256 * @see #setCacheQueries
274- * @see org.hibernate. Query#setCacheRegion
257+ * @see Query#setCacheRegion
275258 * @see Criteria#setCacheRegion
276259 */
277260 public void setQueryCacheRegion (@ Nullable String queryCacheRegion ) {
@@ -359,7 +342,6 @@ public <T> T executeWithNativeSession(HibernateCallback<T> action) {
359342 * @return a result object returned by the action, or {@code null}
360343 * @throws DataAccessException in case of Hibernate errors
361344 */
362- @ SuppressWarnings ("deprecation" )
363345 @ Nullable
364346 protected <T > T doExecute (HibernateCallback <T > action , boolean enforceNativeSession ) throws DataAccessException {
365347 Assert .notNull (action , "Callback object must not be null" );
@@ -374,7 +356,7 @@ protected <T> T doExecute(HibernateCallback<T> action, boolean enforceNativeSess
374356 }
375357 if (session == null ) {
376358 session = obtainSessionFactory ().openSession ();
377- session .setFlushMode (FlushMode .MANUAL );
359+ session .setHibernateFlushMode (FlushMode .MANUAL );
378360 isNew = true ;
379361 }
380362
@@ -543,7 +525,6 @@ public <T> List<T> loadAll(Class<T> entityClass) throws DataAccessException {
543525 }
544526
545527 @ Override
546- @ SuppressWarnings ({"deprecation" })
547528 public void load (Object entity , Serializable id ) throws DataAccessException {
548529 executeWithNativeSession (session -> {
549530 session .load (entity , id );
@@ -887,11 +868,9 @@ public <T> List<T> findByExample(@Nullable String entityName, T exampleEntity, i
887868
888869 @ Deprecated
889870 @ Override
890- @ SuppressWarnings ({"rawtypes" , "unchecked" , "deprecation" })
891871 public List <?> find (String queryString , @ Nullable Object ... values ) throws DataAccessException {
892872 return nonNull (executeWithNativeSession ((HibernateCallback <List <?>>) session -> {
893- org .hibernate .Query queryObject = queryObject (
894- ReflectionUtils .invokeMethod (createQueryMethod , session , queryString ));
873+ Query <?> queryObject = session .createQuery (queryString );
895874 prepareQuery (queryObject );
896875 if (values != null ) {
897876 for (int i = 0 ; i < values .length ; i ++) {
@@ -912,16 +891,14 @@ public List<?> findByNamedParam(String queryString, String paramName, Object val
912891
913892 @ Deprecated
914893 @ Override
915- @ SuppressWarnings ({"rawtypes" , "unchecked" , "deprecation" })
916894 public List <?> findByNamedParam (String queryString , String [] paramNames , Object [] values )
917895 throws DataAccessException {
918896
919897 if (paramNames .length != values .length ) {
920898 throw new IllegalArgumentException ("Length of paramNames array must match length of values array" );
921899 }
922900 return nonNull (executeWithNativeSession ((HibernateCallback <List <?>>) session -> {
923- org .hibernate .Query queryObject = queryObject (
924- ReflectionUtils .invokeMethod (createQueryMethod , session , queryString ));
901+ Query <?> queryObject = session .createQuery (queryString );
925902 prepareQuery (queryObject );
926903 for (int i = 0 ; i < values .length ; i ++) {
927904 applyNamedParameterToQuery (queryObject , paramNames [i ], values [i ]);
@@ -932,12 +909,9 @@ public List<?> findByNamedParam(String queryString, String[] paramNames, Object[
932909
933910 @ Deprecated
934911 @ Override
935- @ SuppressWarnings ({"rawtypes" , "unchecked" , "deprecation" })
936912 public List <?> findByValueBean (String queryString , Object valueBean ) throws DataAccessException {
937-
938913 return nonNull (executeWithNativeSession ((HibernateCallback <List <?>>) session -> {
939- org .hibernate .Query queryObject = queryObject (
940- ReflectionUtils .invokeMethod (createQueryMethod , session , queryString ));
914+ Query <?> queryObject = session .createQuery (queryString );
941915 prepareQuery (queryObject );
942916 queryObject .setProperties (valueBean );
943917 return queryObject .list ();
@@ -951,11 +925,9 @@ public List<?> findByValueBean(String queryString, Object valueBean) throws Data
951925
952926 @ Deprecated
953927 @ Override
954- @ SuppressWarnings ({"rawtypes" , "unchecked" , "deprecation" })
955928 public List <?> findByNamedQuery (String queryName , @ Nullable Object ... values ) throws DataAccessException {
956929 return nonNull (executeWithNativeSession ((HibernateCallback <List <?>>) session -> {
957- org .hibernate .Query queryObject = queryObject (
958- ReflectionUtils .invokeMethod (getNamedQueryMethod , session , queryName ));
930+ Query <?> queryObject = session .getNamedQuery (queryName );
959931 prepareQuery (queryObject );
960932 if (values != null ) {
961933 for (int i = 0 ; i < values .length ; i ++) {
@@ -976,7 +948,6 @@ public List<?> findByNamedQueryAndNamedParam(String queryName, String paramName,
976948
977949 @ Deprecated
978950 @ Override
979- @ SuppressWarnings ({"rawtypes" , "unchecked" , "deprecation" })
980951 public List <?> findByNamedQueryAndNamedParam (
981952 String queryName , @ Nullable String [] paramNames , @ Nullable Object [] values )
982953 throws DataAccessException {
@@ -985,8 +956,7 @@ public List<?> findByNamedQueryAndNamedParam(
985956 throw new IllegalArgumentException ("Length of paramNames array must match length of values array" );
986957 }
987958 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 );
990960 prepareQuery (queryObject );
991961 if (values != null ) {
992962 for (int i = 0 ; i < values .length ; i ++) {
@@ -999,12 +969,9 @@ public List<?> findByNamedQueryAndNamedParam(
999969
1000970 @ Deprecated
1001971 @ Override
1002- @ SuppressWarnings ({"rawtypes" , "unchecked" , "deprecation" })
1003972 public List <?> findByNamedQueryAndValueBean (String queryName , Object valueBean ) throws DataAccessException {
1004-
1005973 return nonNull (executeWithNativeSession ((HibernateCallback <List <?>>) session -> {
1006- org .hibernate .Query queryObject = queryObject (
1007- ReflectionUtils .invokeMethod (getNamedQueryMethod , session , queryName ));
974+ Query <?> queryObject = session .getNamedQuery (queryName );
1008975 prepareQuery (queryObject );
1009976 queryObject .setProperties (valueBean );
1010977 return queryObject .list ();
@@ -1018,11 +985,9 @@ public List<?> findByNamedQueryAndValueBean(String queryName, Object valueBean)
1018985
1019986 @ Deprecated
1020987 @ Override
1021- @ SuppressWarnings ({"rawtypes" , "deprecation" })
1022988 public Iterator <?> iterate (String queryString , @ Nullable Object ... values ) throws DataAccessException {
1023989 return nonNull (executeWithNativeSession ((HibernateCallback <Iterator <?>>) session -> {
1024- org .hibernate .Query queryObject = queryObject (
1025- ReflectionUtils .invokeMethod (createQueryMethod , session , queryString ));
990+ Query <?> queryObject = session .createQuery (queryString );
1026991 prepareQuery (queryObject );
1027992 if (values != null ) {
1028993 for (int i = 0 ; i < values .length ; i ++) {
@@ -1046,11 +1011,9 @@ public void closeIterator(Iterator<?> it) throws DataAccessException {
10461011
10471012 @ Deprecated
10481013 @ Override
1049- @ SuppressWarnings ({"rawtypes" , "deprecation" })
10501014 public int bulkUpdate (String queryString , @ Nullable Object ... values ) throws DataAccessException {
10511015 Integer result = executeWithNativeSession (session -> {
1052- org .hibernate .Query queryObject = queryObject (
1053- ReflectionUtils .invokeMethod (createQueryMethod , session , queryString ));
1016+ Query <?> queryObject = session .createQuery (queryString );
10541017 prepareQuery (queryObject );
10551018 if (values != null ) {
10561019 for (int i = 0 ; i < values .length ; i ++) {
@@ -1079,7 +1042,7 @@ public int bulkUpdate(String queryString, @Nullable Object... values) throws Dat
10791042 * @see FlushMode#MANUAL
10801043 */
10811044 protected void checkWriteOperationAllowed (Session session ) throws InvalidDataAccessApiUsageException {
1082- if (isCheckWriteOperations () && SessionFactoryUtils . getFlushMode ( session ).lessThan (FlushMode .COMMIT )) {
1045+ if (isCheckWriteOperations () && session . getHibernateFlushMode ( ).lessThan (FlushMode .COMMIT )) {
10831046 throw new InvalidDataAccessApiUsageException (
10841047 "Write operations are not allowed in read-only mode (FlushMode.MANUAL): " +
10851048 "Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition." );
@@ -1121,8 +1084,7 @@ protected void prepareCriteria(Criteria criteria) {
11211084 * @see #setCacheQueries
11221085 * @see #setQueryCacheRegion
11231086 */
1124- @ SuppressWarnings ({"rawtypes" , "deprecation" })
1125- protected void prepareQuery (org .hibernate .Query queryObject ) {
1087+ protected void prepareQuery (Query <?> queryObject ) {
11261088 if (isCacheQueries ()) {
11271089 queryObject .setCacheable (true );
11281090 if (getQueryCacheRegion () != null ) {
@@ -1150,9 +1112,7 @@ protected void prepareQuery(org.hibernate.Query queryObject) {
11501112 * @param value the value of the parameter
11511113 * @throws HibernateException if thrown by the Query object
11521114 */
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 )
11561116 throws HibernateException {
11571117
11581118 if (value instanceof Collection ) {
@@ -1166,13 +1126,6 @@ else if (value instanceof Object[]) {
11661126 }
11671127 }
11681128
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-
11761129 private static <T > T nonNull (@ Nullable T result ) {
11771130 Assert .state (result != null , "No result" );
11781131 return result ;
@@ -1193,7 +1146,6 @@ public CloseSuppressingInvocationHandler(Session target) {
11931146 }
11941147
11951148 @ Override
1196- @ SuppressWarnings ({"rawtypes" , "deprecation" })
11971149 @ Nullable
11981150 public Object invoke (Object proxy , Method method , Object [] args ) throws Throwable {
11991151 // Invocation on Session interface coming in...
@@ -1219,8 +1171,8 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
12191171 if (retVal instanceof Criteria ) {
12201172 prepareCriteria (((Criteria ) retVal ));
12211173 }
1222- else if (retVal instanceof org . hibernate . Query ) {
1223- prepareQuery (((org . hibernate . Query ) retVal ));
1174+ else if (retVal instanceof Query ) {
1175+ prepareQuery (((Query <?> ) retVal ));
12241176 }
12251177
12261178 return retVal ;
0 commit comments