【问题标题】:Spring data-jpa and exception handlingSpring data-jpa 和异常处理
【发布时间】:2021-07-22 10:05:40
【问题描述】:

最近我发现了 Spring Data Jpa。我无法让它工作的一件事是正确地将异常转换为 Spring 的异常层次结构。

根据Spring Data JPA forces CGLib proxying to non repository classes<jpa:repositories /> 为使用@Repository 注释的 Spring bean 激活持久性异常转换。本文中的参考文档指向 spring-data-jpa 1.1.1。

但是,当您查看 1.3.0 版的文档时,此段落已被删除。此外,我一直在使用@Repository 注释,尽可能地将它放在任何地方,但没有成功。

我的问题是:最近的 spring-data-jpa lib 版本 1.3.0 是否可以实现正确的异常翻译?

好的。我将在这里放置一些配置:

...

<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  <property name="driverClass" value="oracle.jdbc.OracleDriver"/>
  <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:pbase"/>
  <property name="user" value="sa"/>
  <property name="password" value="pass"/>
</bean>

<context:annotation-config/>

<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="persistenceUnitName" value="prjPersistenceUnit"></property>
    <property name="persistenceXmlLocation" value="classpath:META-INF/mpersistence.xml"></property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"
              p:showSql="true"/>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="myEmf"/>
</bean>

<tx:annotation-driven/>

<jpa:repositories base-package="com.mycompany.repository" />

persistence.xml 的内容

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="prjPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <description>Persistence unit which uses EclipseLink JPA 2.0 implementation.</description>
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>com.mycompany.Setting</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="eclipselink.target-server" value="JBoss"/>
            <property name="eclipselink.target-database" value="Oracle10g"/>
            <property name="eclipselink.weaving" value="static"/>
        </properties>
    </persistence-unit>
</persistence>

我的仓库

@Repository
public interface TestRepository extends JpaRepository<Setting, Long> {

    Setting findByNamee(String name);
}

这里 findByNamee 应该引发一些 Spring 数据库异常,因为数据库中的一个真实属性是 name 而不是 namee。但我总是得到

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException

不过,当使用 @Repository 注释配置常规 Dao 对象时,一切都按预期工作。

我正在尝试使用 eclipseLink 2.3.2 在 Tomcat 6.0 上部署它。

【问题讨论】:

  • 说起来没什么用,但绝对有可能。我在我的项目中使用 1.3.0 版本,并且我有一个测试断言异常翻译适用于我的存储库。拥有&lt;jpa:repositories/&gt; 就足够了。 JpaRepositoryConfigExtension 注册 bean 后处理器 (PersistenceExceptionTranslationPostProcessor),用于查找带有 @Repository 注释的 bean。
  • 谁能指出我错过了什么?

标签: spring spring-data-jpa


【解决方案1】:

您可以查看我从那时起在https://github.com/zagyi/examples/tree/master/spring-data-jpa 发布的一些工作示例代码

【讨论】:

  • 感谢您提供示例,但由于某种原因,它仍然对我不起作用,即使我正在尝试像示例中那样做所有事情。
【解决方案2】:

您必须将 EclipseLink Jpa 方言注入 EntityManagerFactory,因为异常翻译器在方言中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-20
    • 2023-02-10
    • 2022-01-12
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    相关资源
    最近更新 更多