org.springframework.orm.hibernate5

## Class HibernateExceptionTranslator

- java.lang.Object
- - org.springframework.orm.hibernate5.HibernateExceptionTranslator

- All Implemented Interfaces:
    PersistenceExceptionTranslator
  Direct Known Subclasses:
    LocalSessionFactoryBean

      public class HibernateExceptionTranslator
      extends Object
      implements PersistenceExceptionTranslator

  `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.

  Since:
    4.2
  Author:
    Juergen Hoeller
  See Also:
    `PersistenceExceptionTranslationPostProcessor`, `SessionFactoryUtils.convertHibernateAccessException(HibernateException)`, `EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(RuntimeException)`

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `HibernateExceptionTranslator()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected DataAccessException` | `convertHibernateAccessException(HibernateException ex)` Convert the given HibernateException to an appropriate exception from the `org.springframework.dao` hierarchy.
    `void` | `setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator)` Set the JDBC exception translator for Hibernate exception translation purposes.
    `DataAccessException` | `translateExceptionIfPossible(RuntimeException ex)` Translate the given runtime exception thrown by a persistence framework to a corresponding exception from Spring's generic `DataAccessException` hierarchy, if possible.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### HibernateExceptionTranslator

          public HibernateExceptionTranslator()

  - ### Method Detail

    - #### setJdbcExceptionTranslator

          public void setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator)

      Set the JDBC exception translator for Hibernate exception translation purposes.

      Applied to any detected `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:
        `SQLException`, `JDBCException`, `SQLErrorCodeSQLExceptionTranslator`, `SQLStateSQLExceptionTranslator`

    - #### translateExceptionIfPossible

          @Nullable
          public DataAccessException translateExceptionIfPossible(RuntimeException ex)

      Description copied from interface: `PersistenceExceptionTranslator`
      Translate the given runtime exception thrown by a persistence framework to a corresponding exception from Spring's generic `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.

      Specified by:
        `translateExceptionIfPossible` in interface `PersistenceExceptionTranslator`
      Parameters:
        `ex` - a RuntimeException to translate
      Returns:
        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)
      See Also:
        `DataIntegrityViolationException`, `SQLExceptionTranslator`

    - #### convertHibernateAccessException

          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` - the HibernateException that occurred
      Returns:
        a corresponding DataAccessException
      See Also:
        `SessionFactoryUtils.convertHibernateAccessException(org.hibernate.HibernateException)`