Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate Spring dao异常翻译在测试和生产中表现不同_Hibernate_Spring_Orm_Jpa_Exception Handling - Fatal编程技术网

Hibernate Spring dao异常翻译在测试和生产中表现不同

Hibernate Spring dao异常翻译在测试和生产中表现不同,hibernate,spring,orm,jpa,exception-handling,Hibernate,Spring,Orm,Jpa,Exception Handling,我在应用程序中使用Spring异常转换来转换来自底层ORM框架(Hibernate)的异常,我发现ConstraintViolationExceptions在我的测试和生产环境中的转换方式不同 在我的groovy单元测试中,我有以下内容 // provide a non-unique name for a department entity shouldFail(DataIntegrityViolationException, { Department d =

我在应用程序中使用Spring异常转换来转换来自底层ORM框架(Hibernate)的异常,我发现ConstraintViolationExceptions在我的测试和生产环境中的转换方式不同

在我的groovy单元测试中,我有以下内容

    // provide a non-unique name for a department entity
    shouldFail(DataIntegrityViolationException, {

        Department d = new Department(name: 'HR')
        departmentDao.persist(d)
    })
这工作正常-由于基础org.hibernate.exception.ConstraintViolationException引发了预期的DataIntegrityViolationException。在我的groovy服务代码中,我有以下代码块,它应该捕获相同的DataIntegrityViolationException

    try{
        departmentDao.persist(department)
    }
    // There may be a constraint violation if a department with the same name already exists
    catch(DataIntegrityViolationException e){...}
但是,在运行时,Spring不会转换异常,因此运行时Hibernate异常会向堆栈上传播

stacktrace中提到了org.springframework.orm.jpa.JpaSystemException,根据文档,它是“UncategorizedDataAccessException的jpa特定子类,用于与任何具体的org.springframework.dao异常不匹配的jpa系统错误。”

org.hibernate.exception.ConstraintViolationException:无法插入:[com.myapp.Department];嵌套异常为javax.persistence.PersistenceException:org.hibernate.exception.ConstraintViolationException:无法插入:[com.myapp.Department] org.springframework.orm.jpa.JpaSystemException:org.hibernate.exception.ConstraintViolationException:无法插入:[com.myapp.Department];嵌套异常为javax.persistence.PersistenceException:org.hibernate.exception.ConstraintViolationException:无法插入:[com.myapp.Department]

我能看到的唯一区别是,我的测试由HSQLDB支持,而生产代码由MsSql数据库支持。有人能解释为什么异常转换在我的测试和运行时环境中是不同的吗

干杯

编辑:

好的,进一步的细节

我的测试应用程序上下文和生产应用程序上下文都定义PersistenceExceptionTranslationPostProcessor,如下所示

<bean
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

您可以发布测试appcontext和产品appcontext吗?至少包括相关部分,即数据源和transactionmanager配置。是否可能在其中一个包中定义了PersistenceExceptionTranslationPostProcessor bean,而在另一个包中没有定义?是否可以导入其他DataIntegrityViolationException类。有时另一个包包含同名的类。这很难看到。
<!-- HSQL-DB memory database; for testing only -->
<bean id="hsqlDs"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <property name="url" value="jdbc:hsqldb:file:target/db/store;shutdown=true" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>

<bean id="persistenceUnitManager"
    class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    <property name="persistenceXmlLocation" value="classpath:persistence-test.xml" />
    <property name="defaultDataSource" ref="hsqlDs" />
</bean>

<bean id="hsqlEntityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="store" />
    <property name="persistenceUnitManager" ref="persistenceUnitManager" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="generateDdl" value="true" />
            <property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
        </bean>
    </property>
</bean>
<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" />
org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.ConstraintViolationException: could not insert: [com.myapp.Department]; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not insert: [com.myapp.Department]
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:311)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:369)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy233.persist(Unknown Source)