use of org.seasar.doma.jdbc.UniqueConstraintException in project doma-spring-boot by domaframework.
the class DomaPersistenceExceptionTranslator method translateExceptionIfPossible.
@Override
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
if (!(ex instanceof JdbcException)) {
// Fallback to other translators if not JdbcException
return null;
}
if (ex instanceof OptimisticLockException) {
return new OptimisticLockingFailureException(ex.getMessage(), ex);
} else if (ex instanceof UniqueConstraintException) {
return new DuplicateKeyException(ex.getMessage(), ex);
} else if (ex instanceof NonUniqueResultException || ex instanceof NonSingleColumnException) {
return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);
} else if (ex instanceof NoResultException) {
return new EmptyResultDataAccessException(ex.getMessage(), 1, ex);
} else if (ex instanceof UnknownColumnException || ex instanceof ResultMappingException) {
return new TypeMismatchDataAccessException(ex.getMessage(), ex);
}
if (ex.getCause() instanceof SQLException) {
SQLException e = (SQLException) ex.getCause();
String sql = null;
if (ex instanceof SqlExecutionException) {
sql = ((SqlExecutionException) ex).getRawSql();
}
DataAccessException dae = translator.translate(ex.getMessage(), sql, e);
return (dae != null ? dae : new UncategorizedSQLException(ex.getMessage(), sql, e));
}
return new UncategorizedDataAccessException(ex.getMessage(), ex) {
};
}
use of org.seasar.doma.jdbc.UniqueConstraintException in project doma by domaframework.
the class AutoInsertTest method test_UniqueConstraintException.
@Test
public void test_UniqueConstraintException(Config config) throws Exception {
DepartmentDao dao = new DepartmentDaoImpl(config);
Department department = new Department();
department.setDepartmentId(new Identity<Department>(99));
department.setDepartmentNo(99);
department.setDepartmentName("hoge");
int result = dao.insert(department);
assertEquals(1, result);
assertEquals(Integer.valueOf(1), department.getVersion());
try {
dao.insert(department);
fail();
} catch (UniqueConstraintException e) {
}
}
use of org.seasar.doma.jdbc.UniqueConstraintException in project doma by domaframework.
the class ModifyCommand method executeUpdate.
protected int executeUpdate(PreparedStatement preparedStatement) throws SQLException {
try {
int updatedRows = preparedStatement.executeUpdate();
validateRows(updatedRows);
return updatedRows;
} catch (SQLException e) {
Dialect dialect = query.getConfig().getDialect();
if (dialect.isUniqueConstraintViolated(e)) {
throw new UniqueConstraintException(query.getConfig().getExceptionSqlLogType(), sql, e);
}
throw e;
}
}
Aggregations