You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If using both jdbc and hibernate-orm (hibernate 5) in a spring project, only jdbc translates db error codes to custom DataAccessException when configuring them in sql-error-codes.xml.
Hibernate-orm only maps to the standard set of DataAccessException.
In org.springframework.orm.hibernate3.HibernateExceptionTranslator#convertHibernateAccessException the jdbcExceptionTranslator is used.
This allows with the configuration of sql-error-codes.xml to map exceptions to custom DataAccessException 's
In org.springframework.orm.hibernate5.HibernateExceptionTranslator#convertHibernateAccessException (and also the one for hibernate4) this jdbcExceptionTranslator is no longer used.
temporary solved with attached DataAccessExceptionTranslatorHibernateJpaDialect.java
this is then configured as jpaDialect on the entityManagerFactory
What specifically have you found missing in Hibernate's own exception translation? Could you potentially also add this to a custom Hibernate Dialect extension?
When using jdbcExceptionTranslator exceptions are translated to children of org.springframework.dao.DataAccessException
when using hibernates SQLExceptionConverter exceptions are translated to children of org.hibernate.JDBCException
If we want to map a (custom business) exception on the database to an exception in the java code we need 2 different Exception classes (since jdbc and hibernate map from different packages).
If we then want to map this (custom business) exception on the database to a client friendly message on the front end, we also need an ExceptionHandler that catches 2 different Exception classes, which makes it dependent on the way the database is accessed.
In HibernateJpaDialect some hibernate JDBCException are converted to spring DataAccessException (convertHibernateAccessException), and as fallback wrapped in JpaSystemException.
If using hibernates SQLExceptionConverter this would mean that the custom JDBCException would be wrapped in JpaSystemException.
This is a good point indeed: In HibernateExceptionTranslator as well as HibernateJpaDialect, a custom SQLExceptionTranslator can still be useful for translating to custom exception types... even if the standard exception types are largely covered by Hibernate's own mechanism in the meantime, and even if HibernateJpaDialect never really made use of the SQLExceptionTranslator machinery in the first place (just the Hibernate 3.x HibernateExceptionTranslator and LocalSessionFactoryBean did).
For 5.1, I've added a corresponding jdbcExceptionTranslator property, allowing you to specify a custom translator which kicks in before regular JDBCException translation. This can either be a totally custom SQLExceptionTranslator implementation which just handles your custom cases and returns null otherwise (similar to a fallbackTranslator attached to a SQLErrorCodeSQLExceptionTranslator), or one of Spring's standard translators which then overrides Hibernate's regular JDBCException translation completely.
Joram Agten opened SPR-17015 and commented
If using both jdbc and hibernate-orm (hibernate 5) in a spring project, only jdbc translates db error codes to custom
DataAccessException
when configuring them insql-error-codes.xml
.Hibernate-orm only maps to the standard set of
DataAccessException
.In
org.springframework.orm.hibernate3.HibernateExceptionTranslator#convertHibernateAccessException
thejdbcExceptionTranslator
is used.This allows with the configuration of
sql-error-codes.xml
to map exceptions to customDataAccessException
'sIn
org.springframework.orm.hibernate5.HibernateExceptionTranslator#convertHibernateAccessException
(and also the one for hibernate4) thisjdbcExceptionTranslator
is no longer used.temporary solved with attached
DataAccessExceptionTranslatorHibernateJpaDialect.java
this is then configured as
jpaDialect
on theentityManagerFactory
Affects: 4.3.13
Attachments:
Issue Links:
Referenced from: commits d9e8d3b
The text was updated successfully, but these errors were encountered: