Translating PersistenceException into DataAccessException in Spring

I am trying to handle unique key constraint violations in Spring + JPA + Hibernate.

I am using PersistenceExceptionTranslationPostProcessor to DataAccessException PersistenceException into a DataAccessException . When there is a unique key constraint violation, I expect a DuplicateKeyException or a DataIntegrityViolationException , but all I get is a JpaSystemException that wraps a PersistenceException .

Shouldn't the DataAccessException hierarchy be used so that it is small enough to not look for a provider specific error code?

How do I spring DataAccessException PersistenceException into a more specific DataAccessException ?

EDIT: I noticed that this.jpaDialect in DataAccessUtils.translateIfNecessary () is NULL. Is there some kind of setup that I need to configure to set this.jpaDialect to HibernateJpaDialect?

Thanks!

+6
java spring jpa
source share
1 answer

Apparently you don't have the jpaDialect set. For Hibernate, this should look like this:

 <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> </property> <!-- ... --> </bean> 
+9
source share

All Articles