Deprecate outdated HibernateTemplate operations in favor of lambdas
Issue: SPR-16426
This commit is contained in:
parent
c718ddf7b0
commit
4e194c3fbe
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -30,14 +30,37 @@ import org.springframework.dao.DataAccessException;
|
|||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* Interface that specifies a basic set of Hibernate operations,
|
||||
* implemented by {@link HibernateTemplate}. Not often used, but a useful
|
||||
* option to enhance testability, as it can easily be mocked or stubbed.
|
||||
* Interface that specifies a common set of Hibernate operations as well as
|
||||
* a general {@link #execute} method for Session-based lambda expressions.
|
||||
* Implemented by {@link HibernateTemplate}. Not often used, but a useful option
|
||||
* to enhance testability, as it can easily be mocked or stubbed.
|
||||
*
|
||||
* <p>Defines {@code HibernateTemplate}'s data access methods that
|
||||
* mirror various {@link org.hibernate.Session} methods. Users are
|
||||
* strongly encouraged to read the Hibernate {@code Session} javadocs
|
||||
* for details on the semantics of those methods.
|
||||
* <p>Defines {@code HibernateTemplate}'s data access methods that mirror various
|
||||
* {@link org.hibernate.Session} methods. Users are strongly encouraged to read the
|
||||
* Hibernate {@code Session} javadocs for details on the semantics of those methods.
|
||||
*
|
||||
* <p><b>A deprecation note:</b> While {@link HibernateTemplate} and this operations
|
||||
* interface are being kept around for backwards compatibility in terms of the data
|
||||
* access implementation style in Spring applications, we strongly recommend the use
|
||||
* of native {@link org.hibernate.Session} access code for non-trivial interactions.
|
||||
* This in particular affects parameterized queries where - on Java 8+ - a custom
|
||||
* {@link HibernateCallback} lambda code block with {@code createQuery} and several
|
||||
* {@code setParameter} calls on the {@link org.hibernate.query.Query} interface
|
||||
* is an elegant solution, to be executed via the general {@link #execute} method.
|
||||
* All such operations which benefit from a lambda variant have been marked as
|
||||
* {@code deprecated} on this interface.
|
||||
*
|
||||
* <p><b>A Hibernate compatibility note:</b> {@link HibernateTemplate} and the
|
||||
* operations on this interface generally aim to be applicable across all Hibernate
|
||||
* versions. In terms of binary compatibility, Spring ships a variant for each major
|
||||
* generation of Hibernate (in the present case: Hibernate ORM 5.x). However, due to
|
||||
* refactorings and removals in Hibernate ORM 5.3, some variants - in particular
|
||||
* legacy positional parameters starting from index 0 - do not work anymore.
|
||||
* All affected operations are marked as deprecated; please replace them with the
|
||||
* general {@link #execute} method and custom lambda blocks creating the queries,
|
||||
* ideally setting named parameters through {@link org.hibernate.query.Query}.
|
||||
* <b>Please be aware that deprecated operations are known to work with Hibernate
|
||||
* ORM 5.0-5.2 but may not work with Hibernate ORM 5.3 and higher anymore.</b>
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 4.2
|
||||
|
@ -560,116 +583,6 @@ public interface HibernateOperations {
|
|||
void clear() throws DataAccessException;
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Convenience finder methods for HQL strings
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Execute an HQL query, binding a number of values to "?" parameters
|
||||
* in the query string.
|
||||
* @param queryString a query expressed in Hibernate's query language
|
||||
* @param values the values of the parameters
|
||||
* @return a {@link List} containing the results of the query execution
|
||||
* @throws DataAccessException in case of Hibernate errors
|
||||
* @see org.hibernate.Session#createQuery
|
||||
*/
|
||||
List<?> find(String queryString, Object... values) throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Execute an HQL query, binding one value to a ":" named parameter
|
||||
* in the query string.
|
||||
* @param queryString a query expressed in Hibernate's query language
|
||||
* @param paramName the name of the parameter
|
||||
* @param value the value of the parameter
|
||||
* @return a {@link List} containing the results of the query execution
|
||||
* @throws DataAccessException in case of Hibernate errors
|
||||
* @see org.hibernate.Session#getNamedQuery(String)
|
||||
*/
|
||||
List<?> findByNamedParam(String queryString, String paramName, Object value) throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Execute an HQL query, binding a number of values to ":" named
|
||||
* parameters in the query string.
|
||||
* @param queryString a query expressed in Hibernate's query language
|
||||
* @param paramNames the names of the parameters
|
||||
* @param values the values of the parameters
|
||||
* @return a {@link List} containing the results of the query execution
|
||||
* @throws DataAccessException in case of Hibernate errors
|
||||
* @see org.hibernate.Session#getNamedQuery(String)
|
||||
*/
|
||||
List<?> findByNamedParam(String queryString, String[] paramNames, Object[] values) throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Execute an HQL query, binding the properties of the given bean to
|
||||
* <i>named</i> parameters in the query string.
|
||||
* @param queryString a query expressed in Hibernate's query language
|
||||
* @param valueBean the values of the parameters
|
||||
* @return a {@link List} containing the results of the query execution
|
||||
* @throws DataAccessException in case of Hibernate errors
|
||||
* @see org.hibernate.Query#setProperties
|
||||
* @see org.hibernate.Session#createQuery
|
||||
*/
|
||||
List<?> findByValueBean(String queryString, Object valueBean) throws DataAccessException;
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Convenience finder methods for named queries
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Execute a named query binding a number of values to "?" parameters
|
||||
* in the query string.
|
||||
* <p>A named query is defined in a Hibernate mapping file.
|
||||
* @param queryName the name of a Hibernate query in a mapping file
|
||||
* @param values the values of the parameters
|
||||
* @return a {@link List} containing the results of the query execution
|
||||
* @throws DataAccessException in case of Hibernate errors
|
||||
* @see org.hibernate.Session#getNamedQuery(String)
|
||||
*/
|
||||
List<?> findByNamedQuery(String queryName, Object... values) throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Execute a named query, binding one value to a ":" named parameter
|
||||
* in the query string.
|
||||
* <p>A named query is defined in a Hibernate mapping file.
|
||||
* @param queryName the name of a Hibernate query in a mapping file
|
||||
* @param paramName the name of parameter
|
||||
* @param value the value of the parameter
|
||||
* @return a {@link List} containing the results of the query execution
|
||||
* @throws DataAccessException in case of Hibernate errors
|
||||
* @see org.hibernate.Session#getNamedQuery(String)
|
||||
*/
|
||||
List<?> findByNamedQueryAndNamedParam(String queryName, String paramName, Object value)
|
||||
throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Execute a named query, binding a number of values to ":" named
|
||||
* parameters in the query string.
|
||||
* <p>A named query is defined in a Hibernate mapping file.
|
||||
* @param queryName the name of a Hibernate query in a mapping file
|
||||
* @param paramNames the names of the parameters
|
||||
* @param values the values of the parameters
|
||||
* @return a {@link List} containing the results of the query execution
|
||||
* @throws DataAccessException in case of Hibernate errors
|
||||
* @see org.hibernate.Session#getNamedQuery(String)
|
||||
*/
|
||||
List<?> findByNamedQueryAndNamedParam(String queryName, String[] paramNames, Object[] values)
|
||||
throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Execute a named query, binding the properties of the given bean to
|
||||
* ":" named parameters in the query string.
|
||||
* <p>A named query is defined in a Hibernate mapping file.
|
||||
* @param queryName the name of a Hibernate query in a mapping file
|
||||
* @param valueBean the values of the parameters
|
||||
* @return a {@link List} containing the results of the query execution
|
||||
* @throws DataAccessException in case of Hibernate errors
|
||||
* @see org.hibernate.Query#setProperties
|
||||
* @see org.hibernate.Session#getNamedQuery(String)
|
||||
*/
|
||||
List<?> findByNamedQueryAndValueBean(String queryName, Object valueBean) throws DataAccessException;
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Convenience finder methods for detached criteria
|
||||
//-------------------------------------------------------------------------
|
||||
|
@ -758,6 +671,140 @@ public interface HibernateOperations {
|
|||
throws DataAccessException;
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Convenience finder methods for HQL strings
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Execute an HQL query, binding a number of values to "?" parameters
|
||||
* in the query string.
|
||||
* @param queryString a query expressed in Hibernate's query language
|
||||
* @param values the values of the parameters
|
||||
* @return a {@link List} containing the results of the query execution
|
||||
* @throws DataAccessException in case of Hibernate errors
|
||||
* @see org.hibernate.Session#createQuery
|
||||
* @deprecated as of 5.0.4, in favor of a custom {@link HibernateCallback}
|
||||
* lambda code block passed to the general {@link #execute} method
|
||||
*/
|
||||
@Deprecated
|
||||
List<?> find(String queryString, Object... values) throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Execute an HQL query, binding one value to a ":" named parameter
|
||||
* in the query string.
|
||||
* @param queryString a query expressed in Hibernate's query language
|
||||
* @param paramName the name of the parameter
|
||||
* @param value the value of the parameter
|
||||
* @return a {@link List} containing the results of the query execution
|
||||
* @throws DataAccessException in case of Hibernate errors
|
||||
* @see org.hibernate.Session#getNamedQuery(String)
|
||||
* @deprecated as of 5.0.4, in favor of a custom {@link HibernateCallback}
|
||||
* lambda code block passed to the general {@link #execute} method
|
||||
*/
|
||||
@Deprecated
|
||||
List<?> findByNamedParam(String queryString, String paramName, Object value) throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Execute an HQL query, binding a number of values to ":" named
|
||||
* parameters in the query string.
|
||||
* @param queryString a query expressed in Hibernate's query language
|
||||
* @param paramNames the names of the parameters
|
||||
* @param values the values of the parameters
|
||||
* @return a {@link List} containing the results of the query execution
|
||||
* @throws DataAccessException in case of Hibernate errors
|
||||
* @see org.hibernate.Session#getNamedQuery(String)
|
||||
* @deprecated as of 5.0.4, in favor of a custom {@link HibernateCallback}
|
||||
* lambda code block passed to the general {@link #execute} method
|
||||
*/
|
||||
@Deprecated
|
||||
List<?> findByNamedParam(String queryString, String[] paramNames, Object[] values) throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Execute an HQL query, binding the properties of the given bean to
|
||||
* <i>named</i> parameters in the query string.
|
||||
* @param queryString a query expressed in Hibernate's query language
|
||||
* @param valueBean the values of the parameters
|
||||
* @return a {@link List} containing the results of the query execution
|
||||
* @throws DataAccessException in case of Hibernate errors
|
||||
* @see org.hibernate.Query#setProperties
|
||||
* @see org.hibernate.Session#createQuery
|
||||
* @deprecated as of 5.0.4, in favor of a custom {@link HibernateCallback}
|
||||
* lambda code block passed to the general {@link #execute} method
|
||||
*/
|
||||
@Deprecated
|
||||
List<?> findByValueBean(String queryString, Object valueBean) throws DataAccessException;
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Convenience finder methods for named queries
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Execute a named query binding a number of values to "?" parameters
|
||||
* in the query string.
|
||||
* <p>A named query is defined in a Hibernate mapping file.
|
||||
* @param queryName the name of a Hibernate query in a mapping file
|
||||
* @param values the values of the parameters
|
||||
* @return a {@link List} containing the results of the query execution
|
||||
* @throws DataAccessException in case of Hibernate errors
|
||||
* @see org.hibernate.Session#getNamedQuery(String)
|
||||
* @deprecated as of 5.0.4, in favor of a custom {@link HibernateCallback}
|
||||
* lambda code block passed to the general {@link #execute} method
|
||||
*/
|
||||
@Deprecated
|
||||
List<?> findByNamedQuery(String queryName, Object... values) throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Execute a named query, binding one value to a ":" named parameter
|
||||
* in the query string.
|
||||
* <p>A named query is defined in a Hibernate mapping file.
|
||||
* @param queryName the name of a Hibernate query in a mapping file
|
||||
* @param paramName the name of parameter
|
||||
* @param value the value of the parameter
|
||||
* @return a {@link List} containing the results of the query execution
|
||||
* @throws DataAccessException in case of Hibernate errors
|
||||
* @see org.hibernate.Session#getNamedQuery(String)
|
||||
* @deprecated as of 5.0.4, in favor of a custom {@link HibernateCallback}
|
||||
* lambda code block passed to the general {@link #execute} method
|
||||
*/
|
||||
@Deprecated
|
||||
List<?> findByNamedQueryAndNamedParam(String queryName, String paramName, Object value)
|
||||
throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Execute a named query, binding a number of values to ":" named
|
||||
* parameters in the query string.
|
||||
* <p>A named query is defined in a Hibernate mapping file.
|
||||
* @param queryName the name of a Hibernate query in a mapping file
|
||||
* @param paramNames the names of the parameters
|
||||
* @param values the values of the parameters
|
||||
* @return a {@link List} containing the results of the query execution
|
||||
* @throws DataAccessException in case of Hibernate errors
|
||||
* @see org.hibernate.Session#getNamedQuery(String)
|
||||
* @deprecated as of 5.0.4, in favor of a custom {@link HibernateCallback}
|
||||
* lambda code block passed to the general {@link #execute} method
|
||||
*/
|
||||
@Deprecated
|
||||
List<?> findByNamedQueryAndNamedParam(String queryName, String[] paramNames, Object[] values)
|
||||
throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Execute a named query, binding the properties of the given bean to
|
||||
* ":" named parameters in the query string.
|
||||
* <p>A named query is defined in a Hibernate mapping file.
|
||||
* @param queryName the name of a Hibernate query in a mapping file
|
||||
* @param valueBean the values of the parameters
|
||||
* @return a {@link List} containing the results of the query execution
|
||||
* @throws DataAccessException in case of Hibernate errors
|
||||
* @see org.hibernate.Query#setProperties
|
||||
* @see org.hibernate.Session#getNamedQuery(String)
|
||||
* @deprecated as of 5.0.4, in favor of a custom {@link HibernateCallback}
|
||||
* lambda code block passed to the general {@link #execute} method
|
||||
*/
|
||||
@Deprecated
|
||||
List<?> findByNamedQueryAndValueBean(String queryName, Object valueBean) throws DataAccessException;
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Convenience query methods for iteration and bulk updates/deletes
|
||||
//-------------------------------------------------------------------------
|
||||
|
@ -773,7 +820,10 @@ public interface HibernateOperations {
|
|||
* @throws DataAccessException in case of Hibernate errors
|
||||
* @see org.hibernate.Session#createQuery
|
||||
* @see org.hibernate.Query#iterate
|
||||
* @deprecated as of 5.0.4, in favor of a custom {@link HibernateCallback}
|
||||
* lambda code block passed to the general {@link #execute} method
|
||||
*/
|
||||
@Deprecated
|
||||
Iterator<?> iterate(String queryString, Object... values) throws DataAccessException;
|
||||
|
||||
/**
|
||||
|
@ -783,7 +833,10 @@ public interface HibernateOperations {
|
|||
* @param it the {@code Iterator} to close
|
||||
* @throws DataAccessException if the {@code Iterator} could not be closed
|
||||
* @see org.hibernate.Hibernate#close
|
||||
* @deprecated as of 5.0.4, in favor of a custom {@link HibernateCallback}
|
||||
* lambda code block passed to the general {@link #execute} method
|
||||
*/
|
||||
@Deprecated
|
||||
void closeIterator(Iterator<?> it) throws DataAccessException;
|
||||
|
||||
/**
|
||||
|
@ -795,7 +848,10 @@ public interface HibernateOperations {
|
|||
* @throws DataAccessException in case of Hibernate errors
|
||||
* @see org.hibernate.Session#createQuery
|
||||
* @see org.hibernate.Query#executeUpdate
|
||||
* @deprecated as of 5.0.4, in favor of a custom {@link HibernateCallback}
|
||||
* lambda code block passed to the general {@link #execute} method
|
||||
*/
|
||||
@Deprecated
|
||||
int bulkUpdate(String queryString, Object... values) throws DataAccessException;
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -67,12 +67,14 @@ import org.springframework.util.ReflectionUtils;
|
|||
* always be configured as bean in the application context, in the first case
|
||||
* given to the service directly, in the second case to the prepared template.
|
||||
*
|
||||
* <p><b>NOTE: Hibernate access code can also be coded in plain Hibernate style.
|
||||
* Hence, for newly started projects, consider adopting the standard Hibernate
|
||||
* style of coding data access objects instead, based on
|
||||
* {@link SessionFactory#getCurrentSession()}.
|
||||
* This HibernateTemplate primarily exists as a migration helper for Hibernate 3
|
||||
* based data access code, to benefit from bug fixes in Hibernate 5.x.</b>
|
||||
* <p><b>NOTE: Hibernate access code can also be coded against the native Hibernate
|
||||
* {@link Session}. Hence, for newly started projects, consider adopting the standard
|
||||
* Hibernate style of coding against {@link SessionFactory#getCurrentSession()}.</b>
|
||||
* Alternatively, use {@link #execute(HibernateCallback)} with Java 8 lambda code blocks
|
||||
* against the callback-provided {@code Session} which results in elegant code as well,
|
||||
* decoupled from the Hibernate Session lifecycle. The remaining operations on this
|
||||
* HibernateTemplate are deprecated in the meantime and primarily exist as a migration
|
||||
* helper for older Hibernate 3.x/4.x data access code in existing applications.</b>
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 4.2
|
||||
|
@ -821,131 +823,6 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean
|
|||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Convenience finder methods for HQL strings
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
|
||||
public List<?> find(final String queryString, @Nullable final Object... values) throws DataAccessException {
|
||||
return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
|
||||
org.hibernate.Query queryObject = queryObject(
|
||||
ReflectionUtils.invokeMethod(createQueryMethod, session, queryString));
|
||||
prepareQuery(queryObject);
|
||||
if (values != null) {
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
queryObject.setParameter(i, values[i]);
|
||||
}
|
||||
}
|
||||
return queryObject.list();
|
||||
}));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<?> findByNamedParam(String queryString, String paramName, Object value)
|
||||
throws DataAccessException {
|
||||
|
||||
return findByNamedParam(queryString, new String[] {paramName}, new Object[] {value});
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
|
||||
public List<?> findByNamedParam(final String queryString, final String[] paramNames, final Object[] values)
|
||||
throws DataAccessException {
|
||||
|
||||
if (paramNames.length != values.length) {
|
||||
throw new IllegalArgumentException("Length of paramNames array must match length of values array");
|
||||
}
|
||||
return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
|
||||
org.hibernate.Query queryObject = queryObject(
|
||||
ReflectionUtils.invokeMethod(createQueryMethod, session, queryString));
|
||||
prepareQuery(queryObject);
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
applyNamedParameterToQuery(queryObject, paramNames[i], values[i]);
|
||||
}
|
||||
return queryObject.list();
|
||||
}));
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
|
||||
public List<?> findByValueBean(final String queryString, final Object valueBean)
|
||||
throws DataAccessException {
|
||||
|
||||
return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
|
||||
org.hibernate.Query queryObject = queryObject(
|
||||
ReflectionUtils.invokeMethod(createQueryMethod, session, queryString));
|
||||
prepareQuery(queryObject);
|
||||
queryObject.setProperties(valueBean);
|
||||
return queryObject.list();
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Convenience finder methods for named queries
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
|
||||
public List<?> findByNamedQuery(final String queryName, @Nullable final Object... values) throws DataAccessException {
|
||||
return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
|
||||
org.hibernate.Query queryObject = queryObject(
|
||||
ReflectionUtils.invokeMethod(getNamedQueryMethod, session, queryName));
|
||||
prepareQuery(queryObject);
|
||||
if (values != null) {
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
queryObject.setParameter(i, values[i]);
|
||||
}
|
||||
}
|
||||
return queryObject.list();
|
||||
}));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<?> findByNamedQueryAndNamedParam(String queryName, String paramName, Object value)
|
||||
throws DataAccessException {
|
||||
|
||||
return findByNamedQueryAndNamedParam(queryName, new String[] {paramName}, new Object[] {value});
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
|
||||
public List<?> findByNamedQueryAndNamedParam(
|
||||
final String queryName, @Nullable final String[] paramNames, @Nullable final Object[] values)
|
||||
throws DataAccessException {
|
||||
|
||||
if (values != null && (paramNames == null || paramNames.length != values.length)) {
|
||||
throw new IllegalArgumentException("Length of paramNames array must match length of values array");
|
||||
}
|
||||
return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
|
||||
org.hibernate.Query queryObject = (org.hibernate.Query)
|
||||
nonNull(ReflectionUtils.invokeMethod(getNamedQueryMethod, session, queryName));
|
||||
prepareQuery(queryObject);
|
||||
if (values != null) {
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
applyNamedParameterToQuery(queryObject, paramNames[i], values[i]);
|
||||
}
|
||||
}
|
||||
return queryObject.list();
|
||||
}));
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
|
||||
public List<?> findByNamedQueryAndValueBean(final String queryName, final Object valueBean)
|
||||
throws DataAccessException {
|
||||
|
||||
return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
|
||||
org.hibernate.Query queryObject = queryObject(
|
||||
ReflectionUtils.invokeMethod(getNamedQueryMethod, session, queryName));
|
||||
prepareQuery(queryObject);
|
||||
queryObject.setProperties(valueBean);
|
||||
return queryObject.list();
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Convenience finder methods for detached criteria
|
||||
//-------------------------------------------------------------------------
|
||||
|
@ -1012,10 +889,144 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean
|
|||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Convenience finder methods for HQL strings
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
@Deprecated
|
||||
@Override
|
||||
@SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
|
||||
public List<?> find(final String queryString, @Nullable final Object... values) throws DataAccessException {
|
||||
return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
|
||||
org.hibernate.Query queryObject = queryObject(
|
||||
ReflectionUtils.invokeMethod(createQueryMethod, session, queryString));
|
||||
prepareQuery(queryObject);
|
||||
if (values != null) {
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
queryObject.setParameter(i, values[i]);
|
||||
}
|
||||
}
|
||||
return queryObject.list();
|
||||
}));
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
@Override
|
||||
public List<?> findByNamedParam(String queryString, String paramName, Object value)
|
||||
throws DataAccessException {
|
||||
|
||||
return findByNamedParam(queryString, new String[] {paramName}, new Object[] {value});
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
@Override
|
||||
@SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
|
||||
public List<?> findByNamedParam(final String queryString, final String[] paramNames, final Object[] values)
|
||||
throws DataAccessException {
|
||||
|
||||
if (paramNames.length != values.length) {
|
||||
throw new IllegalArgumentException("Length of paramNames array must match length of values array");
|
||||
}
|
||||
return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
|
||||
org.hibernate.Query queryObject = queryObject(
|
||||
ReflectionUtils.invokeMethod(createQueryMethod, session, queryString));
|
||||
prepareQuery(queryObject);
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
applyNamedParameterToQuery(queryObject, paramNames[i], values[i]);
|
||||
}
|
||||
return queryObject.list();
|
||||
}));
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
@Override
|
||||
@SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
|
||||
public List<?> findByValueBean(final String queryString, final Object valueBean)
|
||||
throws DataAccessException {
|
||||
|
||||
return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
|
||||
org.hibernate.Query queryObject = queryObject(
|
||||
ReflectionUtils.invokeMethod(createQueryMethod, session, queryString));
|
||||
prepareQuery(queryObject);
|
||||
queryObject.setProperties(valueBean);
|
||||
return queryObject.list();
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Convenience finder methods for named queries
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
@Deprecated
|
||||
@Override
|
||||
@SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
|
||||
public List<?> findByNamedQuery(final String queryName, @Nullable final Object... values) throws DataAccessException {
|
||||
return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
|
||||
org.hibernate.Query queryObject = queryObject(
|
||||
ReflectionUtils.invokeMethod(getNamedQueryMethod, session, queryName));
|
||||
prepareQuery(queryObject);
|
||||
if (values != null) {
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
queryObject.setParameter(i, values[i]);
|
||||
}
|
||||
}
|
||||
return queryObject.list();
|
||||
}));
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
@Override
|
||||
public List<?> findByNamedQueryAndNamedParam(String queryName, String paramName, Object value)
|
||||
throws DataAccessException {
|
||||
|
||||
return findByNamedQueryAndNamedParam(queryName, new String[] {paramName}, new Object[] {value});
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
@Override
|
||||
@SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
|
||||
public List<?> findByNamedQueryAndNamedParam(
|
||||
final String queryName, @Nullable final String[] paramNames, @Nullable final Object[] values)
|
||||
throws DataAccessException {
|
||||
|
||||
if (values != null && (paramNames == null || paramNames.length != values.length)) {
|
||||
throw new IllegalArgumentException("Length of paramNames array must match length of values array");
|
||||
}
|
||||
return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
|
||||
org.hibernate.Query queryObject = (org.hibernate.Query)
|
||||
nonNull(ReflectionUtils.invokeMethod(getNamedQueryMethod, session, queryName));
|
||||
prepareQuery(queryObject);
|
||||
if (values != null) {
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
applyNamedParameterToQuery(queryObject, paramNames[i], values[i]);
|
||||
}
|
||||
}
|
||||
return queryObject.list();
|
||||
}));
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
@Override
|
||||
@SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
|
||||
public List<?> findByNamedQueryAndValueBean(final String queryName, final Object valueBean)
|
||||
throws DataAccessException {
|
||||
|
||||
return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
|
||||
org.hibernate.Query queryObject = queryObject(
|
||||
ReflectionUtils.invokeMethod(getNamedQueryMethod, session, queryName));
|
||||
prepareQuery(queryObject);
|
||||
queryObject.setProperties(valueBean);
|
||||
return queryObject.list();
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Convenience query methods for iteration and bulk updates/deletes
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
@Deprecated
|
||||
@Override
|
||||
@SuppressWarnings({"rawtypes", "deprecation"})
|
||||
public Iterator<?> iterate(final String queryString, @Nullable final Object... values) throws DataAccessException {
|
||||
|
@ -1032,6 +1043,7 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean
|
|||
}));
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
@Override
|
||||
public void closeIterator(Iterator<?> it) throws DataAccessException {
|
||||
try {
|
||||
|
@ -1042,6 +1054,7 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean
|
|||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
@Override
|
||||
@SuppressWarnings({"rawtypes", "deprecation"})
|
||||
public int bulkUpdate(final String queryString, @Nullable final Object... values) throws DataAccessException {
|
||||
|
@ -1083,35 +1096,6 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the given Query object, applying cache settings and/or
|
||||
* a transaction timeout.
|
||||
* @param queryObject the Query object to prepare
|
||||
* @see #setCacheQueries
|
||||
* @see #setQueryCacheRegion
|
||||
*/
|
||||
@SuppressWarnings({"rawtypes", "deprecation"})
|
||||
protected void prepareQuery(org.hibernate.Query queryObject) {
|
||||
if (isCacheQueries()) {
|
||||
queryObject.setCacheable(true);
|
||||
if (getQueryCacheRegion() != null) {
|
||||
queryObject.setCacheRegion(getQueryCacheRegion());
|
||||
}
|
||||
}
|
||||
if (getFetchSize() > 0) {
|
||||
queryObject.setFetchSize(getFetchSize());
|
||||
}
|
||||
if (getMaxResults() > 0) {
|
||||
queryObject.setMaxResults(getMaxResults());
|
||||
}
|
||||
|
||||
SessionHolder sessionHolder =
|
||||
(SessionHolder) TransactionSynchronizationManager.getResource(obtainSessionFactory());
|
||||
if (sessionHolder != null && sessionHolder.hasTimeout()) {
|
||||
queryObject.setTimeout(sessionHolder.getTimeToLiveInSeconds());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the given Criteria object, applying cache settings and/or
|
||||
* a transaction timeout.
|
||||
|
@ -1140,6 +1124,36 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the given Query object, applying cache settings and/or
|
||||
* a transaction timeout.
|
||||
* @param queryObject the Query object to prepare
|
||||
* @see #setCacheQueries
|
||||
* @see #setQueryCacheRegion
|
||||
*/
|
||||
@Deprecated
|
||||
@SuppressWarnings({"rawtypes", "deprecation"})
|
||||
protected void prepareQuery(org.hibernate.Query queryObject) {
|
||||
if (isCacheQueries()) {
|
||||
queryObject.setCacheable(true);
|
||||
if (getQueryCacheRegion() != null) {
|
||||
queryObject.setCacheRegion(getQueryCacheRegion());
|
||||
}
|
||||
}
|
||||
if (getFetchSize() > 0) {
|
||||
queryObject.setFetchSize(getFetchSize());
|
||||
}
|
||||
if (getMaxResults() > 0) {
|
||||
queryObject.setMaxResults(getMaxResults());
|
||||
}
|
||||
|
||||
SessionHolder sessionHolder =
|
||||
(SessionHolder) TransactionSynchronizationManager.getResource(obtainSessionFactory());
|
||||
if (sessionHolder != null && sessionHolder.hasTimeout()) {
|
||||
queryObject.setTimeout(sessionHolder.getTimeToLiveInSeconds());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the given name parameter to the given Query object.
|
||||
* @param queryObject the Query object
|
||||
|
@ -1147,6 +1161,7 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean
|
|||
* @param value the value of the parameter
|
||||
* @throws HibernateException if thrown by the Query object
|
||||
*/
|
||||
@Deprecated
|
||||
@SuppressWarnings({"rawtypes", "deprecation"})
|
||||
protected void applyNamedParameterToQuery(org.hibernate.Query queryObject, String paramName, Object value)
|
||||
throws HibernateException {
|
||||
|
@ -1163,17 +1178,18 @@ public class HibernateTemplate implements HibernateOperations, InitializingBean
|
|||
}
|
||||
|
||||
|
||||
private static <T> T nonNull(@Nullable T result) {
|
||||
Assert.state(result != null, "No result");
|
||||
return result;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
@SuppressWarnings({"rawtypes", "deprecation"})
|
||||
private static org.hibernate.Query queryObject(@Nullable Object result) {
|
||||
Assert.state(result != null, "No Hibernate Query");
|
||||
return (org.hibernate.Query) result;
|
||||
}
|
||||
|
||||
private static <T> T nonNull(@Nullable T result) {
|
||||
Assert.state(result != null, "No result");
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Invocation handler that suppresses close calls on Hibernate Sessions.
|
||||
|
|
Loading…
Reference in New Issue