0

我正在为数据库应用程序编写 JUnit 测试。针对我的测试 (HSQLDB) 数据库运行测试失败。我想得到的错误消息是这个错误消息,这是实际的异常,我完全理解

org.hsqldb.HsqlException: Violation of unique constraint SYS_PK_47: duplicate value(s) for column(s) SECONDARY_ACCOUNT_ID

但是,此信息不会出现在测试失败的堆栈跟踪中的任何位置。相反,错误似乎是通过 Spring、Hibernate 和 JDBC 过滤的,我得到了这个完全无用、无信息的“JDBC 异常”:

org.springframework.orm.hibernate3.HibernateJdbcException: JDBC exception on Hibernate data access: SQLException for SQL [insert into CAACCTS (CREDIT_APPLICATION_ID, PRIMARY_ACCOUNT_NUMBER, SECONDARY_ACCOUNT_ID, SECONDARY_ACCT_NAME, SECONDARY_ADDY_ID, SECONDARY_ACCOUNT_NUMBER) values (?, ?, ?, ?, ?, ?)]; SQL state [null]; error code [0]; Could not execute JDBC batch update; nested exception is org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:651)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:104)
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:516)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.data.jpa.repository.support.LockModeRepositoryPostProcessor$LockModePopulatingMethodIntercceptor.invoke(LockModeRepositoryPostProcessor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at $Proxy26.save(Unknown Source)
    [snip]

我已经将 Log4J 日志设置到 TRACE 没有成功。谁能告诉我如何抓住这个原始错误(不仅在这个失败的测试中,而且在未来的测试中也是如此?)

我正在使用 HSQLDB、Spring 3、Hibernate 3.6 和 JPA 2。我的 IDE 是 Intellij IDEA。该项目是基于 Maven 的。如果需要帮助我诊断此问题,我将很乐意提供更多详细信息。

4

2 回答 2

0

我认为最好捕获org.hibernate.JDBCException或检查 Hibernate 异常是否是org.hibernate.JDBCException的实例:

} catch (JDBCException e) {
    logger.warn("jdbc problem", e.getSQLException());
}

或者...

if (e instanceof JDBCException) {
    SQLException sqle = ((JDBCException) e).getSQLException();
}

通常休眠 JDBCException 包含相关的 DB 特定 JDBC 异常,您可以使用 getSQLException() 方法检索该异常

于 2014-06-06T16:16:08.580 回答
0

一种深入到根异常的方法是这样的:

catch(Throwable t)
{
    while(t.getCause != null)
    {
        t = t.getCause();
    }
    t.printStackTrace(System.out);
}
于 2013-07-24T18:37:19.833 回答