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 に答える