Koen Serneels
Koen Serneels

Reputation: 91

How to deactivate Spring Data exception translation

The new org.springframework.orm.hibernate5.HibernateExceptionTranslator uses a fallthrough in the sense that it first tries to map the exception using vanilla Hibernate (SessionFactoryUtils). If no translation could be done it tries JPA via EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible().

The latter troubles us as it is translating IllegalStateException to InvalidDataAccessApiUsageException. I'm not really sure why IllegalStateException and IllegalArgumentException get this special treatment, but ok. So now we suddenly end up with translated exceptions that weren't translated before. Since these extra translations are in context of JPA (as it is called convertJpaAccessExceptionIfPossible) this also makes no sense as we are not using JPA but vanilla Hibernate.

So, what is the correct way to make sure that we only get Hibernate exceptions translated? Afaic the HibernateExceptionTranslator has no options to turn off JPA translation and the LocalSessionFactoryBean extends HibernateExceptionTranslator without the option to inject another implementation. Wouldn't it make more sense to have the HibernateExceptionTranslator JPA unaware and add another translator in case JPA is actually used?

Upvotes: 4

Views: 1749

Answers (2)

Koen Serneels
Koen Serneels

Reputation: 91

Please see https://jira.spring.io/browse/SPR-14681 for the appropriate answer(s)

Upvotes: 0

Journeycorner
Journeycorner

Reputation: 2542

From Spring Data reference:

Using this element looks up Spring Data repositories as described in Creating repository instances. Beyond that it activates persistence exception translation for all beans annotated with @Repository to let exceptions being thrown by the JPA persistence providers be converted into Spring’s DataAccessException hierarchy.

Omit the @Repository annotation, then vanilla Hibernate exceptions won't get translated.

Upvotes: 2

Related Questions