Question

    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);
    }

When I have duplicate key error err.getMessage() returns org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch

In the stacktrace there is this error too: java.sql.BatchUpdateException: ORA-00001: unique constraint (ACM.SCH_UK) violated

How can I get this ORA-00001 message as a string, instead of the org.hibernate.exception.ConstraintViolationException text?

Was it helpful?

Solution

java.sql.BatchUpdateException is the root cause of your PersistenceException, you can extract it as follows:

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

.

ret = getRootCause(ex).getMessage();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top