Java – convert persistenceexception to dataaccessexception in spring

I am trying to handle unique key constraint violations in spring JPA hibernate environment

I use persistenceexception translation postprocessor to convert persistenceexception to dataaccessexception If there is a unique key constraint violation, I will throw a duplicatekeyexception or dataintegrity violationexception, but I get a jpasystemexception wrapped with persistenceexception

The point of using the dataaccessexception hierarchy is that is it fine enough not to look for vendor specific error codes?

How can I have spring convert persistenceexception to a more specific dataaccessexception?

Editor: I noticed dataaccessutils This. In translateifnecessary() Jpadialect is null I need to configure some settings to set this Is jpadialect set to hibernate jpadialect?

thank you!

Solution

Obviously you didn't set up jpadialect 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>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>