问题描述
我正在尝试在Spring + JPA + Hibernate环境中处理唯一的键约束违规.
I'm trying to handle unique key constraint violations in a Spring + JPA + Hibernate environment.
我使用PersistenceExceptionTranslationPostProcessor
将PersistenceException
转换为DataAccessException
.当发生唯一的键约束冲突时,我希望抛出一个DuplicateKeyException
或一个DataIntegrityViolationException
,但是我得到的只是一个包裹了PersistenceException
的JpaSystemException
.
I use PersistenceExceptionTranslationPostProcessor
to translate a PersistenceException
to a DataAccessException
. When there's a unique key constraint violation, I'd expect a DuplicateKeyException
or a DataIntegrityViolationException
thrown, but all I get is a JpaSystemException
that wraps a PersistenceException
.
使用DataAccessException
层次结构的全部意义不是很细致,而不必查找特定于供应商的错误代码吗?
Isn't the whole point of using the DataAccessException
hierarchy that it's fine-grained enough not to have to look up the vendor-specific error code?
我如何让Spring将PersistenceException
转换为更具体的DataAccessException
?
How do I have Spring translate a PersistenceException
to a more specific DataAccessException
?
我注意到DataAccessUtils.translateIfNecessary()中的this.jpaDialect为空.我需要配置一些设置以将this.jpaDialect设置为HibernateJpaDialect吗?
I noticed that this.jpaDialect in DataAccessUtils.translateIfNecessary() is null. Is there some setting I need to configure to set this.jpaDialect to HibernateJpaDialect?
谢谢!
推荐答案
显然您没有设置jpaDialect
.对于Hibernate,它应如下所示:
Apparently you don't have jpaDialect
set. For Hibernate it 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>
这篇关于在Spring中将PersistenceException转换为DataAccessException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!