Specific deprecations and consistent setter varargs in Hibernate support

Issue: SPR-11291
This commit is contained in:
Juergen Hoeller
2014-01-27 22:11:58 +01:00
parent 09ad8347be
commit 7e2167fd17
10 changed files with 99 additions and 76 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 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.
@@ -289,7 +289,7 @@ public abstract class HibernateAccessor implements InitializingBean, BeanFactory
* @see org.hibernate.Session#enableFilter(String)
* @see LocalSessionFactoryBean#setFilterDefinitions
*/
public void setFilterNames(String[] filterNames) {
public void setFilterNames(String... filterNames) {
this.filterNames = filterNames;
}
@@ -464,8 +464,8 @@ public abstract class HibernateAccessor implements InitializingBean, BeanFactory
protected void enableFilters(Session session) {
String[] filterNames = getFilterNames();
if (filterNames != null) {
for (int i = 0; i < filterNames.length; i++) {
session.enableFilter(filterNames[i]);
for (String filterName : filterNames) {
session.enableFilter(filterName);
}
}
}
@@ -479,8 +479,8 @@ public abstract class HibernateAccessor implements InitializingBean, BeanFactory
protected void disableFilters(Session session) {
String[] filterNames = getFilterNames();
if (filterNames != null) {
for (int i = 0; i < filterNames.length; i++) {
session.disableFilter(filterNames[i]);
for (String filterName : filterNames) {
session.disableFilter(filterName);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 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.
@@ -36,7 +36,7 @@ import org.springframework.transaction.support.TransactionSynchronizationManager
* Hibernate's own {@code SessionFactory.getCurrentSession()} method, to be
* able to detect a thread-bound Session. Typically, the code will look like as follows:
*
* <pre>
* <pre class="code">
* public void doSomeDataAccessAction() {
* Session session = this.sessionFactory.getCurrentSession();
* ...
@@ -68,7 +68,12 @@ import org.springframework.transaction.support.TransactionSynchronizationManager
* @see org.hibernate.SessionFactory#getCurrentSession()
* @see HibernateTransactionManager
* @see HibernateTemplate
* @deprecated as of Spring 3.2.7, in favor of either HibernateTemplate usage or
* native Hibernate API usage within transactions, in combination with a general
* {@link org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor}.
* Note: This class does not have an equivalent replacement in {@code orm.hibernate4}.
*/
@Deprecated
public class HibernateInterceptor extends HibernateAccessor implements MethodInterceptor {
private boolean exceptionConversionEnabled = true;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 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.
@@ -90,10 +90,13 @@ public interface HibernateOperations {
* {@link List}.
* <p>This is a convenience method for executing Hibernate find calls or
* queries within an action.
* @param action calback object that specifies the Hibernate action
* @param action callback object that specifies the Hibernate action
* @return a List result returned by the action, or {@code null}
* @throws org.springframework.dao.DataAccessException in case of Hibernate errors
* @deprecated as of Spring 3.2.7, in favor of using a regular {@link #execute}
* call with a generic List type declared
*/
@Deprecated
List executeFind(HibernateCallback<?> action) throws DataAccessException;
@@ -131,8 +134,7 @@ public interface HibernateOperations {
* @throws org.springframework.dao.DataAccessException in case of Hibernate errors
* @see org.hibernate.Session#get(Class, java.io.Serializable, org.hibernate.LockMode)
*/
<T> T get(Class<T> entityClass, Serializable id, LockMode lockMode)
throws DataAccessException;
<T> T get(Class<T> entityClass, Serializable id, LockMode lockMode) throws DataAccessException;
/**
* Return the persistent instance of the given entity class
@@ -164,8 +166,7 @@ public interface HibernateOperations {
* @throws org.springframework.dao.DataAccessException in case of Hibernate errors
* @see org.hibernate.Session#get(Class, java.io.Serializable, org.hibernate.LockMode)
*/
Object get(String entityName, Serializable id, LockMode lockMode)
throws DataAccessException;
Object get(String entityName, Serializable id, LockMode lockMode) throws DataAccessException;
/**
* Return the persistent instance of the given entity class
@@ -199,8 +200,7 @@ public interface HibernateOperations {
* @throws org.springframework.dao.DataAccessException in case of Hibernate errors
* @see org.hibernate.Session#load(Class, java.io.Serializable)
*/
<T> T load(Class<T> entityClass, Serializable id, LockMode lockMode)
throws DataAccessException;
<T> T load(Class<T> entityClass, Serializable id, LockMode lockMode) throws DataAccessException;
/**
* Return the persistent instance of the given entity class
@@ -234,8 +234,7 @@ public interface HibernateOperations {
* @throws org.springframework.dao.DataAccessException in case of Hibernate errors
* @see org.hibernate.Session#load(Class, java.io.Serializable)
*/
Object load(String entityName, Serializable id, LockMode lockMode)
throws DataAccessException;
Object load(String entityName, Serializable id, LockMode lockMode) throws DataAccessException;
/**
* Return all persistent instances of the given entity class.
@@ -245,7 +244,7 @@ public interface HibernateOperations {
* @throws org.springframework.dao.DataAccessException if there is a Hibernate error
* @see org.hibernate.Session#createCriteria
*/
<T>List<T> loadAll(Class<T> entityClass) throws DataAccessException;
<T> List<T> loadAll(Class<T> entityClass) throws DataAccessException;
/**
* Load the persistent instance with the given identifier

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 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.
@@ -132,7 +132,7 @@ public class HibernateTemplate extends HibernateAccessor implements HibernateOpe
/**
* Create a new HibernateTemplate instance.
* @param sessionFactory SessionFactory to create Sessions
* @param sessionFactory the SessionFactory to create Sessions with
*/
public HibernateTemplate(SessionFactory sessionFactory) {
setSessionFactory(sessionFactory);
@@ -141,7 +141,7 @@ public class HibernateTemplate extends HibernateAccessor implements HibernateOpe
/**
* Create a new HibernateTemplate instance.
* @param sessionFactory SessionFactory to create Sessions
* @param sessionFactory the SessionFactory to create Sessions with
* @param allowCreate if a non-transactional Session should be created when no
* transactional Session can be found for the current thread
*/
@@ -339,6 +339,7 @@ public class HibernateTemplate extends HibernateAccessor implements HibernateOpe
return doExecute(action, false, false);
}
@Deprecated
public List executeFind(HibernateCallback<?> action) throws DataAccessException {
Object result = doExecute(action, false, false);
if (result != null && !(result instanceof List)) {
@@ -480,17 +481,17 @@ public class HibernateTemplate extends HibernateAccessor implements HibernateOpe
* @see #prepareCriteria
*/
protected Session createSessionProxy(Session session) {
Class[] sessionIfcs = null;
Class mainIfc = (session instanceof org.hibernate.classic.Session ?
Class<?>[] sessionIfcs;
Class<?> mainIfc = (session instanceof org.hibernate.classic.Session ?
org.hibernate.classic.Session.class : Session.class);
if (session instanceof EventSource) {
sessionIfcs = new Class[] {mainIfc, EventSource.class};
sessionIfcs = new Class<?>[] {mainIfc, EventSource.class};
}
else if (session instanceof SessionImplementor) {
sessionIfcs = new Class[] {mainIfc, SessionImplementor.class};
sessionIfcs = new Class<?>[] {mainIfc, SessionImplementor.class};
}
else {
sessionIfcs = new Class[] {mainIfc};
sessionIfcs = new Class<?>[] {mainIfc};
}
return (Session) Proxy.newProxyInstance(
session.getClass().getClassLoader(), sessionIfcs,

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 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.
@@ -276,7 +276,7 @@ public class LocalSessionFactoryBean extends AbstractSessionFactoryBean implemen
* resources are specified locally via this bean.
* @see org.hibernate.cfg.Configuration#configure(java.net.URL)
*/
public void setConfigLocations(Resource[] configLocations) {
public void setConfigLocations(Resource... configLocations) {
this.configLocations = configLocations;
}
@@ -290,7 +290,7 @@ public class LocalSessionFactoryBean extends AbstractSessionFactoryBean implemen
* @see #setMappingLocations
* @see org.hibernate.cfg.Configuration#addResource
*/
public void setMappingResources(String[] mappingResources) {
public void setMappingResources(String... mappingResources) {
this.mappingResources = mappingResources;
}
@@ -303,7 +303,7 @@ public class LocalSessionFactoryBean extends AbstractSessionFactoryBean implemen
* or to specify all mappings locally.
* @see org.hibernate.cfg.Configuration#addInputStream
*/
public void setMappingLocations(Resource[] mappingLocations) {
public void setMappingLocations(Resource... mappingLocations) {
this.mappingLocations = mappingLocations;
}
@@ -316,7 +316,7 @@ public class LocalSessionFactoryBean extends AbstractSessionFactoryBean implemen
* or to specify all mappings locally.
* @see org.hibernate.cfg.Configuration#addCacheableFile(java.io.File)
*/
public void setCacheableMappingLocations(Resource[] cacheableMappingLocations) {
public void setCacheableMappingLocations(Resource... cacheableMappingLocations) {
this.cacheableMappingLocations = cacheableMappingLocations;
}
@@ -327,7 +327,7 @@ public class LocalSessionFactoryBean extends AbstractSessionFactoryBean implemen
* or to specify all mappings locally.
* @see org.hibernate.cfg.Configuration#addJar(java.io.File)
*/
public void setMappingJarLocations(Resource[] mappingJarLocations) {
public void setMappingJarLocations(Resource... mappingJarLocations) {
this.mappingJarLocations = mappingJarLocations;
}
@@ -338,7 +338,7 @@ public class LocalSessionFactoryBean extends AbstractSessionFactoryBean implemen
* or to specify all mappings locally.
* @see org.hibernate.cfg.Configuration#addDirectory(java.io.File)
*/
public void setMappingDirectoryLocations(Resource[] mappingDirectoryLocations) {
public void setMappingDirectoryLocations(Resource... mappingDirectoryLocations) {
this.mappingDirectoryLocations = mappingDirectoryLocations;
}
@@ -456,7 +456,7 @@ public class LocalSessionFactoryBean extends AbstractSessionFactoryBean implemen
* @see TypeDefinitionBean
* @see org.hibernate.cfg.Mappings#addTypeDef(String, String, java.util.Properties)
*/
public void setTypeDefinitions(TypeDefinitionBean[] typeDefinitions) {
public void setTypeDefinitions(TypeDefinitionBean... typeDefinitions) {
this.typeDefinitions = typeDefinitions;
}
@@ -470,7 +470,7 @@ public class LocalSessionFactoryBean extends AbstractSessionFactoryBean implemen
* @see FilterDefinitionFactoryBean
* @see org.hibernate.cfg.Configuration#addFilterDefinition
*/
public void setFilterDefinitions(FilterDefinition[] filterDefinitions) {
public void setFilterDefinitions(FilterDefinition... filterDefinitions) {
this.filterDefinitions = filterDefinitions;
}

View File

@@ -611,10 +611,12 @@ public abstract class SessionFactoryUtils {
*/
public static void applyTransactionTimeout(Criteria criteria, SessionFactory sessionFactory) {
Assert.notNull(criteria, "No Criteria object specified");
SessionHolder sessionHolder =
(SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
if (sessionHolder != null && sessionHolder.hasTimeout()) {
criteria.setTimeout(sessionHolder.getTimeToLiveInSeconds());
if (sessionFactory != null) {
SessionHolder sessionHolder =
(SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
if (sessionHolder != null && sessionHolder.hasTimeout()) {
criteria.setTimeout(sessionHolder.getTimeToLiveInSeconds());
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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.
@@ -42,10 +42,7 @@ import org.springframework.util.ClassUtils;
/**
* Subclass of Spring's standard LocalSessionFactoryBean for Hibernate,
* supporting JDK 1.5+ annotation metadata for mappings.
*
* <p>Note: This class requires Hibernate 3.2 or later, with the
* Java Persistence API and the Hibernate Annotations add-on present.
* supporting annotation metadata for mappings.
*
* <p>Example for an AnnotationSessionFactoryBean bean definition:
*
@@ -68,6 +65,9 @@ import org.springframework.util.ClassUtils;
* &lt;property name="packagesToScan" value="test.package"/&gt;
* &lt;/bean&gt;</pre>
*
* <p>Note: This class requires Hibernate 3.2 or later, with the
* Java Persistence API and the Hibernate Annotations add-on present.
*
* @author Juergen Hoeller
* @since 1.2.2
* @see #setDataSource
@@ -82,7 +82,7 @@ public class AnnotationSessionFactoryBean extends LocalSessionFactoryBean implem
private static final String PACKAGE_INFO_SUFFIX = ".package-info";
private Class[] annotatedClasses;
private Class<?>[] annotatedClasses;
private String[] annotatedPackages;
@@ -113,19 +113,19 @@ public class AnnotationSessionFactoryBean extends LocalSessionFactoryBean implem
/**
* Specify annotated classes, for which mappings will be read from
* class-level JDK 1.5+ annotation metadata.
* class-level annotation metadata.
* @see org.hibernate.cfg.AnnotationConfiguration#addAnnotatedClass(Class)
*/
public void setAnnotatedClasses(Class[] annotatedClasses) {
public void setAnnotatedClasses(Class<?>... annotatedClasses) {
this.annotatedClasses = annotatedClasses;
}
/**
* Specify the names of annotated packages, for which package-level
* JDK 1.5+ annotation metadata will be read.
* annotation metadata will be read.
* @see org.hibernate.cfg.AnnotationConfiguration#addPackage(String)
*/
public void setAnnotatedPackages(String[] annotatedPackages) {
public void setAnnotatedPackages(String... annotatedPackages) {
this.annotatedPackages = annotatedPackages;
}
@@ -136,7 +136,7 @@ public class AnnotationSessionFactoryBean extends LocalSessionFactoryBean implem
* classes in the classpath. This is analogous to Spring's component-scan feature
* ({@link org.springframework.context.annotation.ClassPathBeanDefinitionScanner}).
*/
public void setPackagesToScan(String[] packagesToScan) {
public void setPackagesToScan(String... packagesToScan) {
this.packagesToScan = packagesToScan;
}
@@ -148,7 +148,7 @@ public class AnnotationSessionFactoryBean extends LocalSessionFactoryBean implem
* Hibernate's special {@code @org.hibernate.annotations.Entity}.
* @see #setPackagesToScan
*/
public void setEntityTypeFilters(TypeFilter[] entityTypeFilters) {
public void setEntityTypeFilters(TypeFilter... entityTypeFilters) {
this.entityTypeFilters = entityTypeFilters;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 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.
@@ -46,6 +46,11 @@ import org.springframework.orm.hibernate3.SessionFactoryUtils;
* by default. A custom HibernateTemplate instance can be used through overriding
* {@link #createHibernateTemplate}.
*
* <p><b>NOTE: As of Hibernate 3.0.1, transactional Hibernate access code can
* also be coded in plain Hibernate style. Hence, for newly started projects,
* consider adopting the standard Hibernate3 style of coding data access objects
* instead, based on {@link org.hibernate.SessionFactory#getCurrentSession()}.</b>
*
* @author Juergen Hoeller
* @since 1.2
* @see #setSessionFactory
@@ -105,8 +110,8 @@ public abstract class HibernateDaoSupport extends DaoSupport {
* You may introspect its configuration, but not modify the configuration
* (other than from within an {@link #initDao} implementation).
* Consider creating a custom HibernateTemplate instance via
* {@code new HibernateTemplate(getSessionFactory())}, in which
* case you're allowed to customize the settings on the resulting instance.
* {@code new HibernateTemplate(getSessionFactory())}, in which case
* you're allowed to customize the settings on the resulting instance.
*/
public final HibernateTemplate getHibernateTemplate() {
return this.hibernateTemplate;
@@ -136,10 +141,10 @@ public abstract class HibernateDaoSupport extends DaoSupport {
* @throws DataAccessResourceFailureException if the Session couldn't be created
* @throws IllegalStateException if no thread-bound Session found and allowCreate=false
* @see org.springframework.orm.hibernate3.SessionFactoryUtils#getSession(SessionFactory, boolean)
* @deprecated as of Spring 3.2.7, in favor of {@link HibernateTemplate} usage
*/
protected final Session getSession()
throws DataAccessResourceFailureException, IllegalStateException {
@Deprecated
protected final Session getSession() throws DataAccessResourceFailureException, IllegalStateException {
return getSession(this.hibernateTemplate.isAllowCreate());
}
@@ -161,9 +166,11 @@ public abstract class HibernateDaoSupport extends DaoSupport {
* @throws DataAccessResourceFailureException if the Session couldn't be created
* @throws IllegalStateException if no thread-bound Session found and allowCreate=false
* @see org.springframework.orm.hibernate3.SessionFactoryUtils#getSession(SessionFactory, boolean)
* @deprecated as of Spring 3.2.7, in favor of {@link HibernateTemplate} usage
*/
@Deprecated
protected final Session getSession(boolean allowCreate)
throws DataAccessResourceFailureException, IllegalStateException {
throws DataAccessResourceFailureException, IllegalStateException {
return (!allowCreate ?
SessionFactoryUtils.getSession(getSessionFactory(), false) :
@@ -185,7 +192,9 @@ public abstract class HibernateDaoSupport extends DaoSupport {
* @param ex HibernateException that occurred
* @return the corresponding DataAccessException instance
* @see org.springframework.orm.hibernate3.SessionFactoryUtils#convertHibernateAccessException
* @deprecated as of Spring 3.2.7, in favor of {@link HibernateTemplate} usage
*/
@Deprecated
protected final DataAccessException convertHibernateAccessException(HibernateException ex) {
return this.hibernateTemplate.convertHibernateAccessException(ex);
}
@@ -197,7 +206,9 @@ public abstract class HibernateDaoSupport extends DaoSupport {
* {@link #getSession} and {@link #convertHibernateAccessException}.
* @param session the Session to close
* @see org.springframework.orm.hibernate3.SessionFactoryUtils#releaseSession
* @deprecated as of Spring 3.2.7, in favor of {@link HibernateTemplate} usage
*/
@Deprecated
protected final void releaseSession(Session session) {
SessionFactoryUtils.releaseSession(session, getSessionFactory());
}