使用 EclipseLinkJpaDialect 在 Spring 中将 PersistenceException 转换为 DataAccessException

发布于 2024-11-13 07:58:41 字数 2436 浏览 4 评论 0原文

你好。 我有同样的问题解释here

主要区别是我使用 EclipseLink 作为JpaProvider。 所以我将我的entityManagerFactory配置如下:

<bean id="entityManagerFactoryCont0" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath:META-INF/contratto-persistence-cont0.xml" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"/>
    </property>
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
    </property>
</bean> 

我注意到EclipseLink不会覆盖DefaultJpaDialect类的translateExceptionIfPossible方法,这很糟糕......

所以在我的测试用例中我有一个独特的约束冲突异常并且调试控制流显示了这个堆栈跟踪:

EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(RuntimeException) line: 282   
EclipseLinkJpaDialect(DefaultJpaDialect).translateExceptionIfPossible(RuntimeException) line: 120   
LocalContainerEntityManagerFactoryBean(AbstractEntityManagerFactoryBean).translateExceptionIfPossible(RuntimeException) line: 368   
ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(RuntimeException) line: 58   
DataAccessUtils.translateIfNecessary(RuntimeException, PersistenceExceptionTranslator) line: 213    
PersistenceExceptionTranslationInterceptor.invoke(MethodInvocation) line: 163   
ReflectiveMethodInvocation.proceed() line: 172

调用的异常是:

javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.1.2.v20101206-r8635): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: [BEA][Oracle JDBC Driver][Oracle]ORA-00001: unique constraint (JSP_OWN.IDX_MC_CC_RAPPORTI_02) violated

返回的最终异常是一般的 JpaSystemException:

org.springframework.orm.jpa.JpaSystemException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.1.2.v20101206-r8635): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: violata restrizione di unicità (CONT_OWN.PK_CONT_EVENT_MESSAGE)

知道如何让机器为 EclipseLink 工作吗? 亲切的问候

马西莫

Hallo.
I have the same issue explained here

The main difference is that I use EclipseLink as JpaProvider.
So I configured my entityManagerFactory as below:

<bean id="entityManagerFactoryCont0" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath:META-INF/contratto-persistence-cont0.xml" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"/>
    </property>
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
    </property>
</bean> 

I noticed that EclipseLink does not override the translateExceptionIfPossible method of DefaultJpaDialect class, and this is bad...

So in my test case I have a unique constraint violation exception and debugging the control flow shows this stacktrace:

EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(RuntimeException) line: 282   
EclipseLinkJpaDialect(DefaultJpaDialect).translateExceptionIfPossible(RuntimeException) line: 120   
LocalContainerEntityManagerFactoryBean(AbstractEntityManagerFactoryBean).translateExceptionIfPossible(RuntimeException) line: 368   
ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(RuntimeException) line: 58   
DataAccessUtils.translateIfNecessary(RuntimeException, PersistenceExceptionTranslator) line: 213    
PersistenceExceptionTranslationInterceptor.invoke(MethodInvocation) line: 163   
ReflectiveMethodInvocation.proceed() line: 172

The exception of the call is:

javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.1.2.v20101206-r8635): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: [BEA][Oracle JDBC Driver][Oracle]ORA-00001: unique constraint (JSP_OWN.IDX_MC_CC_RAPPORTI_02) violated

The final exception that is returned is the general JpaSystemException:

org.springframework.orm.jpa.JpaSystemException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.1.2.v20101206-r8635): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: violata restrizione di unicità (CONT_OWN.PK_CONT_EVENT_MESSAGE)

Any idea how to get the machine works for EclipseLink?
Kind regards

Massimo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文