Example usage for org.hibernate.exception ConstraintViolationException getSQLState

List of usage examples for org.hibernate.exception ConstraintViolationException getSQLState

Introduction

In this page you can find the example usage for org.hibernate.exception ConstraintViolationException getSQLState.

Prototype

public String getSQLState() 

Source Link

Document

Get the X/Open or ANSI SQL SQLState error code from the underlying SQLException .

Usage

From source file:de.micromata.genome.jpa.EmgrFactory.java

License:Apache License

/**
 * Convert exception./*from  w ww. j  a  v a 2 s .co  m*/
 *
 * @param ex the ex
 * @return the runtime exception
 */
public static RuntimeException convertException(RuntimeException ex) {
    if (ex instanceof QueryTimeoutException) {
        // this is a oracle/hibernate bug workouround.
        // hibernate think this is is a query timeout, but should a DataException
        if (ex.getCause() instanceof org.hibernate.QueryTimeoutException) {
            org.hibernate.QueryTimeoutException qto = (org.hibernate.QueryTimeoutException) ex.getCause();
            if (qto.getCause() instanceof SQLException) {
                SQLException sqlex = (SQLException) qto.getCause();
                // ORA-12899
                if (sqlex.getErrorCode() == 12899) {
                    return new DataPersistenceException(ex.getMessage(), qto.getSQL(), sqlex.getSQLState(), ex);
                }
            }
        }
    }
    if (ex instanceof PersistenceException) {
        Throwable cause = ex.getCause();
        if (cause instanceof ConstraintViolationException) {
            ConstraintViolationException cve = (ConstraintViolationException) cause;
            cve.getMessage();
            String sql = cve.getSQL();
            return new ConstraintPersistenceException(cve.getMessage(), sql, cve.getSQLState(),
                    cve.getConstraintName(), ex);
        } else if (cause instanceof DataException) {
            DataException dex = (DataException) cause;
            return new DataPersistenceException(ex.getMessage(), dex.getSQL(), dex.getSQLState(), ex);
        } else if (cause instanceof PropertyValueException) {
            if (StringUtils.startsWith(cause.getMessage(), "not-null ") == true) {
                return new NullableConstraintPersistenceException(ex.getMessage(), ex);
            }
        }
    }
    return ex;
}

From source file:de.phoenix.webresource.util.AbstractPhoenixResource.java

License:Open Source License

private boolean isDuplicateEntryError(ConstraintViolationException e) {
    return e.getErrorCode() == DUPLICATE_SQL_ERROR && e.getSQLState().equals(DUPLICATE_SQL_STATE);
}

From source file:es.logongas.ix3.dao.impl.ExceptionTranslator.java

License:Apache License

public List<BusinessMessage> getBusinessMessages(org.hibernate.exception.ConstraintViolationException cve) {
    List<BusinessMessage> businessMessages = new ArrayList<BusinessMessage>();
    BusinessMessage businessMessage;//from  w  w  w . jav  a  2s  . co  m

    String message;
    int errorCode;
    String sqlState;

    if (cve.getSQLException() != null) {
        message = cve.getSQLException().getLocalizedMessage();
        errorCode = cve.getSQLException().getErrorCode();
        sqlState = cve.getSQLException().getSQLState();
    } else {
        message = cve.getLocalizedMessage();
        errorCode = cve.getErrorCode();
        sqlState = cve.getSQLState();
    }

    es.logongas.ix3.core.database.ConstraintViolation constraintViolation = constraintViolationTranslator
            .translate(message, errorCode, sqlState);

    if (constraintViolation == null) {
        throw cve;
    } else {
        businessMessage = new BusinessMessage(constraintViolation.getMessage());
    }

    businessMessages.add(businessMessage);

    return businessMessages;
}

From source file:org.candlepin.util.RdbmsExceptionTranslator.java

License:Open Source License

public boolean isConstraintViolationDuplicateEntry(PersistenceException pe) {
    log.debug("Translating {}", pe);

    if (pe.getCause() != null && pe.getCause() instanceof ConstraintViolationException) {

        ConstraintViolationException cve = (ConstraintViolationException) pe.getCause();
        log.info("ConstraintViolationException error code:" + cve.getErrorCode());

        //MySQL error code ER_DUP_ENTRY
        //http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
        if (cve.getSQLState() != null && cve.getSQLState().equals("23000") && cve.getErrorCode() == 1062) {
            return true;
        }//from w w  w .  java2s  .  c  o  m

        //Postgres error code DUPLICATE OBJECT
        //https://www.postgresql.org/docs/8.3/static/errcodes-appendix.html
        if (cve.getSQLState() != null && cve.getSQLState().equals("23505")) {
            return true;
        }

        //TODO add Oracle
    }
    return false;
}