1
    try {
        if (schId != null) {
            log.info(">>> save");
            schedule = em.merge(schedule);

            em.persist(schedule);
        } else {
            em.persist(schedule);
        }

        em.flush();
        ret = "ok";
    } catch (Exception err) {
        ret = err.getMessage();
        err.printStackTrace();

        facesMessages.addFromResourceBundle(Severity.ERROR, "databaseError", ret);
    }

当我有重复的键错误err.getMessage()返回org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch

在堆栈跟踪中也有这个错误: java.sql.BatchUpdateException: ORA-00001: unique constraint (ACM.SCH_UK) violated

我怎样才能得到这个 ORA-00001 消息作为一个字符串,而不是org.hibernate.exception.ConstraintViolationException文本?

4

1 回答 1

2

java.sql.BatchUpdateException是您的根本原因PersistenceException,您可以将其提取如下:

public static Throwable getRootCause(Throwable ex) {
    while (true) {
        Throwable cause = ex.getCause();
        if (cause == null) return ex;
        ex = cause;
    }
}

.

ret = getRootCause(ex).getMessage();
于 2010-12-28T13:38:29.077 回答