Hibernate 4 LocalSessionFactoryBean implements PersistenceExceptionTranslator interface as well (SPR-8952); consistent extending of HibernateExceptionTranslator for Hibernate 3 as well as Hibernate 4

This commit is contained in:
Juergen Hoeller 2011-12-21 15:27:53 +01:00 committed by Chris Beams
parent 1c9fe623f9
commit 392247d674
4 changed files with 38 additions and 86 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2010 the original author or authors. * Copyright 2002-2011 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -21,15 +21,11 @@ import javax.sql.DataSource;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.JDBCException;
import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;
import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.jdbc.support.SQLExceptionTranslator;
/** /**
* Abstract {@link org.springframework.beans.factory.FactoryBean} that creates * Abstract {@link org.springframework.beans.factory.FactoryBean} that creates
@ -54,8 +50,8 @@ import org.springframework.jdbc.support.SQLExceptionTranslator;
* @see org.hibernate.SessionFactory#getCurrentSession() * @see org.hibernate.SessionFactory#getCurrentSession()
* @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor * @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
*/ */
public abstract class AbstractSessionFactoryBean public abstract class AbstractSessionFactoryBean extends HibernateExceptionTranslator
implements FactoryBean<SessionFactory>, InitializingBean, DisposableBean, PersistenceExceptionTranslator { implements FactoryBean<SessionFactory>, InitializingBean, DisposableBean {
/** Logger available to subclasses */ /** Logger available to subclasses */
protected final Log logger = LogFactory.getLog(getClass()); protected final Log logger = LogFactory.getLog(getClass());
@ -66,8 +62,6 @@ public abstract class AbstractSessionFactoryBean
private boolean exposeTransactionAwareSessionFactory = true; private boolean exposeTransactionAwareSessionFactory = true;
private SQLExceptionTranslator jdbcExceptionTranslator;
private SessionFactory sessionFactory; private SessionFactory sessionFactory;
@ -184,23 +178,6 @@ public abstract class AbstractSessionFactoryBean
return this.exposeTransactionAwareSessionFactory; return this.exposeTransactionAwareSessionFactory;
} }
/**
* Set the JDBC exception translator for the SessionFactory,
* exposed via the PersistenceExceptionTranslator interface.
* <p>Applied to any SQLException root cause of a Hibernate JDBCException,
* overriding Hibernate's default SQLException translation (which is
* based on Hibernate's Dialect for a specific target database).
* @param jdbcExceptionTranslator the exception translator
* @see java.sql.SQLException
* @see org.hibernate.JDBCException
* @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
* @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator
* @see org.springframework.dao.support.PersistenceExceptionTranslator
*/
public void setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator) {
this.jdbcExceptionTranslator = jdbcExceptionTranslator;
}
/** /**
* Build and expose the SessionFactory. * Build and expose the SessionFactory.
@ -269,41 +246,6 @@ public abstract class AbstractSessionFactoryBean
} }
/**
* Implementation of the PersistenceExceptionTranslator interface,
* as autodetected by Spring's PersistenceExceptionTranslationPostProcessor.
* <p>Converts the exception if it is a HibernateException;
* else returns <code>null</code> to indicate an unknown exception.
* @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
* @see #convertHibernateAccessException
*/
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
if (ex instanceof HibernateException) {
return convertHibernateAccessException((HibernateException) ex);
}
return null;
}
/**
* Convert the given HibernateException to an appropriate exception from the
* <code>org.springframework.dao</code> hierarchy.
* <p>Will automatically apply a specified SQLExceptionTranslator to a
* Hibernate JDBCException, else rely on Hibernate's default translation.
* @param ex HibernateException that occured
* @return a corresponding DataAccessException
* @see SessionFactoryUtils#convertHibernateAccessException
* @see #setJdbcExceptionTranslator
*/
protected DataAccessException convertHibernateAccessException(HibernateException ex) {
if (this.jdbcExceptionTranslator != null && ex instanceof JDBCException) {
JDBCException jdbcEx = (JDBCException) ex;
return this.jdbcExceptionTranslator.translate(
"Hibernate operation: " + jdbcEx.getMessage(), jdbcEx.getSQL(), jdbcEx.getSQLException());
}
return SessionFactoryUtils.convertHibernateAccessException(ex);
}
/** /**
* Build the underlying Hibernate SessionFactory. * Build the underlying Hibernate SessionFactory.
* @return the raw SessionFactory (potentially to be wrapped with a * @return the raw SessionFactory (potentially to be wrapped with a

View File

@ -18,6 +18,7 @@ package org.springframework.orm.hibernate3;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.JDBCException; import org.hibernate.JDBCException;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.PersistenceExceptionTranslator; import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.jdbc.support.SQLExceptionTranslator; import org.springframework.jdbc.support.SQLExceptionTranslator;
@ -26,14 +27,15 @@ import org.springframework.jdbc.support.SQLExceptionTranslator;
* {@link PersistenceExceptionTranslator} capable of translating {@link HibernateException} * {@link PersistenceExceptionTranslator} capable of translating {@link HibernateException}
* instances to Spring's {@link DataAccessException} hierarchy. * instances to Spring's {@link DataAccessException} hierarchy.
* *
* <p>When configuring the Spring container via XML, note that this translator is * <p>Extended by {@link LocalSessionFactoryBean}, so there is no need to declare this
* automatically used internally by {@link SessionFactoryBean} types. When configuring * translator in addition to a {@code LocalSessionFactoryBean}.
* the container with {@code @Configuration} classes, a {@code @Bean} of this type
* must be registered manually.
* *
* <p>When configuring the container with {@code @Configuration} classes, a {@code @Bean}
* of this type must be registered manually.
*
* @author Juergen Hoeller
* @author Chris Beams * @author Chris Beams
* @since 3.1 * @since 3.1
* @see SessionFactoryBuilder
* @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor * @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
* @see SessionFactoryUtils#convertHibernateAccessException(HibernateException) * @see SessionFactoryUtils#convertHibernateAccessException(HibernateException)
* @see SQLExceptionTranslator * @see SQLExceptionTranslator
@ -42,6 +44,25 @@ public class HibernateExceptionTranslator implements PersistenceExceptionTransla
private SQLExceptionTranslator jdbcExceptionTranslator; private SQLExceptionTranslator jdbcExceptionTranslator;
/**
* Set the JDBC exception translator for the SessionFactory,
* exposed via the PersistenceExceptionTranslator interface.
* <p>Applied to any SQLException root cause of a Hibernate JDBCException,
* overriding Hibernate's default SQLException translation (which is
* based on Hibernate's Dialect for a specific target database).
* @param jdbcExceptionTranslator the exception translator
* @see java.sql.SQLException
* @see org.hibernate.JDBCException
* @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
* @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator
* @see org.springframework.dao.support.PersistenceExceptionTranslator
*/
public void setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator) {
this.jdbcExceptionTranslator = jdbcExceptionTranslator;
}
public DataAccessException translateExceptionIfPossible(RuntimeException ex) { public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
if (ex instanceof HibernateException) { if (ex instanceof HibernateException) {
return convertHibernateAccessException((HibernateException) ex); return convertHibernateAccessException((HibernateException) ex);
@ -68,21 +89,4 @@ public class HibernateExceptionTranslator implements PersistenceExceptionTransla
return SessionFactoryUtils.convertHibernateAccessException(ex); return SessionFactoryUtils.convertHibernateAccessException(ex);
} }
/**
* Set the JDBC exception translator for the SessionFactory,
* exposed via the PersistenceExceptionTranslator interface.
* <p>Applied to any SQLException root cause of a Hibernate JDBCException,
* overriding Hibernate's default SQLException translation (which is
* based on Hibernate's Dialect for a specific target database).
* @param jdbcExceptionTranslator the exception translator
* @see java.sql.SQLException
* @see org.hibernate.JDBCException
* @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
* @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator
* @see org.springframework.dao.support.PersistenceExceptionTranslator
*/
public void setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator) {
this.jdbcExceptionTranslator = jdbcExceptionTranslator;
}
} }

View File

@ -23,7 +23,13 @@ import org.springframework.dao.support.PersistenceExceptionTranslator;
/** /**
* {@link PersistenceExceptionTranslator} capable of translating {@link HibernateException} * {@link PersistenceExceptionTranslator} capable of translating {@link HibernateException}
* instances to Spring's {@link org.springframework.dao.DataAccessException} hierarchy. * instances to Spring's {@link DataAccessException} hierarchy.
*
* <p>Extended by {@link LocalSessionFactoryBean}, so there is no need to declare this
* translator in addition to a {@code LocalSessionFactoryBean}.
*
* <p>When configuring the container with {@code @Configuration} classes, a {@code @Bean}
* of this type must be registered manually.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 3.1 * @since 3.1

View File

@ -53,8 +53,8 @@ import org.springframework.core.io.support.ResourcePatternUtils;
* @see #setDataSource * @see #setDataSource
* @see #setPackagesToScan * @see #setPackagesToScan
*/ */
public class LocalSessionFactoryBean implements FactoryBean<SessionFactory>, ResourceLoaderAware, public class LocalSessionFactoryBean extends HibernateExceptionTranslator
InitializingBean, DisposableBean { implements FactoryBean<SessionFactory>, ResourceLoaderAware, InitializingBean, DisposableBean {
private DataSource dataSource; private DataSource dataSource;