Introduced HibernateTemplate for Hibernate 4 as a migration helper

Note that this variant of HibernateTemplate is stripped down in terms of Session management options and just provides a current-session style with a fallback to a temporary session-per-operation.  Furthermore, in the latter fallback mode, only read operations are supported, just like with a JPA EntityManager.

Issue: SPR-11291
This commit is contained in:
Juergen Hoeller
2014-01-27 21:37:52 +01:00
parent cc0a845653
commit 79e17dbfa0
10 changed files with 3486 additions and 28 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;
}
@@ -466,8 +466,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);
}
}
}
@@ -481,8 +481,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.
@@ -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-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.
@@ -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<Object> 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.

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.
@@ -40,6 +40,7 @@ import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.event.EventSource;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
@@ -132,7 +133,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 +142,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
*/
@@ -341,6 +342,7 @@ public class HibernateTemplate extends HibernateAccessor implements HibernateOpe
}
@Override
@Deprecated
@SuppressWarnings("unchecked")
public List<Object> executeFind(HibernateCallback<?> action) throws DataAccessException {
Object result = doExecute(action, false, false);
@@ -1077,7 +1079,7 @@ public class HibernateTemplate extends HibernateAccessor implements HibernateOpe
final String queryName, final String[] paramNames, final Object[] values)
throws DataAccessException {
if (paramNames != null && values != null && paramNames.length != values.length) {
if (values != null && (paramNames == null || paramNames.length != values.length)) {
throw new IllegalArgumentException("Length of paramNames array must match length of values array");
}
return executeWithNativeSession(new HibernateCallback<List<Object>>() {

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());
}