Search in sources :

Example 1 with NonUniqueResultException

use of org.seasar.doma.jdbc.NonUniqueResultException 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) {
    };
}
Also used : NonUniqueResultException(org.seasar.doma.jdbc.NonUniqueResultException) NonSingleColumnException(org.seasar.doma.jdbc.NonSingleColumnException) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) OptimisticLockException(org.seasar.doma.jdbc.OptimisticLockException) NoResultException(org.seasar.doma.jdbc.NoResultException) ResultMappingException(org.seasar.doma.jdbc.ResultMappingException) JdbcException(org.seasar.doma.jdbc.JdbcException) TypeMismatchDataAccessException(org.springframework.dao.TypeMismatchDataAccessException) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) UncategorizedDataAccessException(org.springframework.dao.UncategorizedDataAccessException) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) UnknownColumnException(org.seasar.doma.jdbc.UnknownColumnException) SqlExecutionException(org.seasar.doma.jdbc.SqlExecutionException) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) UniqueConstraintException(org.seasar.doma.jdbc.UniqueConstraintException) IncorrectResultSizeDataAccessException(org.springframework.dao.IncorrectResultSizeDataAccessException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) DataAccessException(org.springframework.dao.DataAccessException) IncorrectResultSizeDataAccessException(org.springframework.dao.IncorrectResultSizeDataAccessException) TypeMismatchDataAccessException(org.springframework.dao.TypeMismatchDataAccessException) UncategorizedDataAccessException(org.springframework.dao.UncategorizedDataAccessException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Example 2 with NonUniqueResultException

use of org.seasar.doma.jdbc.NonUniqueResultException in project doma by domaframework.

the class BasicSingleResultHandlerTest method testHandle_NonUniqueResultException.

@Test
public void testHandle_NonUniqueResultException() throws Exception {
    MockResultSetMetaData metaData = new MockResultSetMetaData();
    metaData.columns.add(new ColumnMetaData("x"));
    MockResultSet resultSet = new MockResultSet(metaData);
    resultSet.rows.add(new RowData("aaa"));
    resultSet.rows.add(new RowData("bbb"));
    SqlFileSelectQuery query = new SqlFileSelectQuery();
    query.setConfig(runtimeConfig);
    query.setSqlFilePath(SqlFileUtil.buildPath(getClass().getName(), method.getName()));
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setMethod(method);
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    BasicSingleResultHandler<String> handler = new BasicSingleResultHandler<>(StringWrapper::new);
    try {
        handler.handle(resultSet, query, (i, next) -> {
        });
        fail();
    } catch (NonUniqueResultException ignore) {
    }
}
Also used : NonUniqueResultException(org.seasar.doma.jdbc.NonUniqueResultException) StringWrapper(org.seasar.doma.wrapper.StringWrapper) RowData(org.seasar.doma.internal.jdbc.mock.RowData) SqlFileSelectQuery(org.seasar.doma.jdbc.query.SqlFileSelectQuery) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) Test(org.junit.jupiter.api.Test)

Example 3 with NonUniqueResultException

use of org.seasar.doma.jdbc.NonUniqueResultException in project doma by domaframework.

the class DomainSingleResultHandlerTest method testHandle_NonUniqueResultException.

@Test
public void testHandle_NonUniqueResultException() throws Exception {
    MockResultSetMetaData metaData = new MockResultSetMetaData();
    metaData.columns.add(new ColumnMetaData("phoneNumber"));
    MockResultSet resultSet = new MockResultSet(metaData);
    resultSet.rows.add(new RowData("01-2345-6789"));
    resultSet.rows.add(new RowData("02-2345-6789"));
    SqlFileSelectQuery query = new SqlFileSelectQuery();
    query.setConfig(runtimeConfig);
    query.setSqlFilePath(SqlFileUtil.buildPath(getClass().getName(), method.getName()));
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setMethod(method);
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    DomainSingleResultHandler<String, PhoneNumber> handler = new DomainSingleResultHandler<>(_PhoneNumber.getSingletonInternal());
    try {
        handler.handle(resultSet, query, (i, next) -> {
        });
        fail();
    } catch (NonUniqueResultException expected) {
    }
}
Also used : NonUniqueResultException(org.seasar.doma.jdbc.NonUniqueResultException) RowData(org.seasar.doma.internal.jdbc.mock.RowData) SqlFileSelectQuery(org.seasar.doma.jdbc.query.SqlFileSelectQuery) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) PhoneNumber(example.domain.PhoneNumber) example.domain._PhoneNumber(example.domain._PhoneNumber) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) Test(org.junit.jupiter.api.Test)

Example 4 with NonUniqueResultException

use of org.seasar.doma.jdbc.NonUniqueResultException in project doma by domaframework.

the class EntitySingleResultHandlerTest method testHandle_NonUniqueResultException.

@Test
public void testHandle_NonUniqueResultException() throws Exception {
    MockResultSetMetaData metaData = new MockResultSetMetaData();
    metaData.columns.add(new ColumnMetaData("id"));
    metaData.columns.add(new ColumnMetaData("name"));
    MockResultSet resultSet = new MockResultSet(metaData);
    resultSet.rows.add(new RowData(1, "aaa"));
    resultSet.rows.add(new RowData(2, "bbb"));
    SqlFileSelectQuery query = new SqlFileSelectQuery();
    query.setConfig(runtimeConfig);
    query.setSqlFilePath(SqlFileUtil.buildPath(getClass().getName(), method.getName()));
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setMethod(method);
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    EntitySingleResultHandler<Emp> handler = new EntitySingleResultHandler<>(_Emp.getSingletonInternal());
    try {
        handler.handle(resultSet, query, (i, next) -> {
        });
        fail();
    } catch (NonUniqueResultException expected) {
    }
}
Also used : NonUniqueResultException(org.seasar.doma.jdbc.NonUniqueResultException) RowData(org.seasar.doma.internal.jdbc.mock.RowData) SqlFileSelectQuery(org.seasar.doma.jdbc.query.SqlFileSelectQuery) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) Emp(example.entity.Emp) example.entity._Emp(example.entity._Emp) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) Test(org.junit.jupiter.api.Test)

Aggregations

NonUniqueResultException (org.seasar.doma.jdbc.NonUniqueResultException)4 Test (org.junit.jupiter.api.Test)3 ColumnMetaData (org.seasar.doma.internal.jdbc.mock.ColumnMetaData)3 MockResultSet (org.seasar.doma.internal.jdbc.mock.MockResultSet)3 MockResultSetMetaData (org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData)3 RowData (org.seasar.doma.internal.jdbc.mock.RowData)3 SqlFileSelectQuery (org.seasar.doma.jdbc.query.SqlFileSelectQuery)3 PhoneNumber (example.domain.PhoneNumber)1 example.domain._PhoneNumber (example.domain._PhoneNumber)1 Emp (example.entity.Emp)1 example.entity._Emp (example.entity._Emp)1 SQLException (java.sql.SQLException)1 JdbcException (org.seasar.doma.jdbc.JdbcException)1 NoResultException (org.seasar.doma.jdbc.NoResultException)1 NonSingleColumnException (org.seasar.doma.jdbc.NonSingleColumnException)1 OptimisticLockException (org.seasar.doma.jdbc.OptimisticLockException)1 ResultMappingException (org.seasar.doma.jdbc.ResultMappingException)1 SqlExecutionException (org.seasar.doma.jdbc.SqlExecutionException)1 UniqueConstraintException (org.seasar.doma.jdbc.UniqueConstraintException)1 UnknownColumnException (org.seasar.doma.jdbc.UnknownColumnException)1