Top Description Fields Constructors Methods
org.springframework.orm.hibernate5

public Class HibernateExceptionTranslator

extends Object
implements PersistenceExceptionTranslator
Class Inheritance
All Implemented Interfaces
org.springframework.dao.support.PersistenceExceptionTranslator
Known Direct Subclasses
org.springframework.orm.hibernate5.LocalSessionFactoryBean
Imports
jakarta.persistence.PersistenceException, org.hibernate.HibernateException, .JDBCException, org.springframework.dao.DataAccessException, org.springframework.dao.support.PersistenceExceptionTranslator, org.springframework.jdbc.support.SQLExceptionTranslator, org.springframework.lang.Nullable, org.springframework.orm.jpa.EntityManagerFactoryUtils

PersistenceExceptionTranslator capable of translating HibernateException instances to Spring's DataAccessException hierarchy. As of Spring 4.3.2 and Hibernate 5.2, it also converts standard JPA PersistenceException instances.

Extended by LocalSessionFactoryBean, so there is no need to declare this translator in addition to a LocalSessionFactoryBean.

When configuring the container with @Configuration classes, a @Bean of this type must be registered manually.

Author
Juergen Hoeller
Since
4.2
See Also
org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor, SessionFactoryUtils#convertHibernateAccessException(HibernateException), EntityManagerFactoryUtils#convertJpaAccessExceptionIfPossible(RuntimeException)

Field Summary

Modifier and TypeField and Description
private SQLExceptionTranslator

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected DataAccessException

Returns:

a corresponding DataAccessException
convertHibernateAccessException
(HibernateException
the HibernateException that occurred
ex
)

Convert the given HibernateException to an appropriate exception from the org.springframework.dao hierarchy.

public void
setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator)

Set the JDBC exception translator for Hibernate exception translation purposes.

public DataAccessException
translateExceptionIfPossible(RuntimeException
a RuntimeException to translate
ex
)

Implements org.springframework.dao.support.PersistenceExceptionTranslator.translateExceptionIfPossible.

Translate the given runtime exception thrown by a persistence framework to a corresponding exception from Spring's generic org.springframework.dao.DataAccessException hierarchy, if possible.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

jdbcExceptionTranslatorback to summary
private SQLExceptionTranslator jdbcExceptionTranslator
Annotations
@Nullable

Constructor Detail

HibernateExceptionTranslatorback to summary
public HibernateExceptionTranslator()

Method Detail

convertHibernateAccessExceptionback to summary
protected DataAccessException convertHibernateAccessException(HibernateException ex)

Convert the given HibernateException to an appropriate exception from the org.springframework.dao hierarchy.

Will automatically apply a specified SQLExceptionTranslator to a Hibernate JDBCException, otherwise rely on Hibernate's default translation.

Parameters
ex:HibernateException

the HibernateException that occurred

Returns:DataAccessException

a corresponding DataAccessException

See Also
SessionFactoryUtils#convertHibernateAccessException
setJdbcExceptionTranslatorback to summary
public void setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator)

Set the JDBC exception translator for Hibernate exception translation purposes.

Applied to any detected java.sql.SQLException root cause of a Hibernate JDBCException, overriding Hibernate's own SQLException translation (which is based on a Hibernate Dialect for a specific target database).

Since
5.1
See Also
java.sql.SQLException, org.hibernate.JDBCException, org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator, org.springframework.jdbc.support.SQLStateSQLExceptionTranslator
translateExceptionIfPossibleback to summary
public DataAccessException translateExceptionIfPossible(RuntimeException ex)

Implements org.springframework.dao.support.PersistenceExceptionTranslator.translateExceptionIfPossible.

Doc from org.springframework.dao.support.PersistenceExceptionTranslator.translateExceptionIfPossible.

Translate the given runtime exception thrown by a persistence framework to a corresponding exception from Spring's generic org.springframework.dao.DataAccessException hierarchy, if possible.

Do not translate exceptions that are not understood by this translator: for example, if coming from another persistence framework, or resulting from user code or otherwise unrelated to persistence.

Of particular importance is the correct translation to DataIntegrityViolationException, for example on constraint violation. Implementations may use Spring JDBC's sophisticated exception translation to provide further information in the event of SQLException as a root cause.

Parameters
ex:RuntimeException

a RuntimeException to translate

Returns:DataAccessException

the corresponding DataAccessException (or null if the exception could not be translated, as in this case it may result from user code rather than from an actual persistence problem)

Annotations
@Override
@Nullable