JPA事务异常获得真正原因 - 嵌套异常

时间:2016-10-05 11:26:18

标签: java sql jpa exception openjpa

我使用openjpa并使用store和commit。

提交有时会启动异常,但我无法获得更多精确度。

有时候,我猜这是完整性问题(相同日期存储两次)。

错误消息和堆栈是:

The transaction has been rolled back.  See the nested exceptions for details on the errors that occurred.
<openjpa-2.4.1-r422266:1730418 fatal store error> org.apache.openjpa.persistence.RollbackException: The transaction has been rolled back.  See the nested exceptions for details on the errors that occurred.
...

但是在哪里可以找到更详细的原因,或者如何获取嵌套异常

由于

1 个答案:

答案 0 :(得分:1)

出现同样的问题,JPA提交将我的JPA持久调用中的异常嵌套并掩埋它们,因为我只使用了e.getMessage()

public static List<String> getExceptionMessageChain(Throwable throwable) {
   List<String> result = new ArrayList<String>();
   while (throwable != null) {
      result.add(throwable.getMessage());
      throwable = throwable.getCause();
   }
   return result; //["THIRD EXCEPTION", "SECOND EXCEPTION", "FIRST EXCEPTION"]
}

catch(Exception e) {
   // JPA Exceptions can be nested, need to get entire chain
   getExceptionMessageChain(e).forEach(System.out::println);
}

来源: Get detail messages of chained exceptions Java