@Test public void testThrowIncorrectResultSizeDataAccessException() { { DataAccessException dataAccessException = translator .translateExceptionIfPossible(new NonUniqueResultException( SqlLogType.FORMATTED, SqlKind.SELECT, "select * from todo where created_at = ?", "select * from todo where created_at = '2016-03-06'", "TodoDao/findBy.sql")); assertThat(dataAccessException, is(instanceOf(IncorrectResultSizeDataAccessException.class))); } { DataAccessException dataAccessException = translator .translateExceptionIfPossible(new NonSingleColumnException( SqlLogType.FORMATTED, SqlKind.SELECT, "select todo_id, title from todo where created_at = ?", "select todo_id, title from todo where created_at = '2016-03-06'", "TodoDao/findBy.sql")); assertThat(dataAccessException, is(instanceOf(IncorrectResultSizeDataAccessException.class))); } }
@Override public DataAccessException translateExceptionIfPossible(RuntimeException ex) { if (!(ex instanceof 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(); } return translator.translate(ex.getMessage(), sql, e); } return new UncategorizedDataAccessException(ex.getMessage(), ex) { }; }
DomaPersistenceExceptionTranslator implements PersistenceExceptionTranslator { @Override public DataAccessException translateExceptionIfPossible(RuntimeException ex) { if (!(ex instanceof 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(); } return translator.translate(ex.getMessage(), sql, e); } return new UncategorizedDataAccessException(ex.getMessage(), ex) { }; } }
DomaPersistenceExceptionTranslator implements PersistenceExceptionTranslator { @Override public DataAccessException translateExceptionIfPossible(RuntimeException ex) { if (!(ex instanceof 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(); } return translator.translate(ex.getMessage(), sql, e); } return new UncategorizedDataAccessException(ex.getMessage(), ex) { }; } DomaPersistenceExceptionTranslator(
			SQLExceptionTranslator sqlExceptionTranslator); }
DomaPersistenceExceptionTranslator implements PersistenceExceptionTranslator { @Override public DataAccessException translateExceptionIfPossible(RuntimeException ex) { if (!(ex instanceof 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(); } return translator.translate(ex.getMessage(), sql, e); } return new UncategorizedDataAccessException(ex.getMessage(), ex) { }; } DomaPersistenceExceptionTranslator(
			SQLExceptionTranslator sqlExceptionTranslator); @Override DataAccessException translateExceptionIfPossible(RuntimeException ex); }
DomaPersistenceExceptionTranslator implements PersistenceExceptionTranslator { @Override public DataAccessException translateExceptionIfPossible(RuntimeException ex) { if (!(ex instanceof 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(); } return translator.translate(ex.getMessage(), sql, e); } return new UncategorizedDataAccessException(ex.getMessage(), ex) { }; } DomaPersistenceExceptionTranslator(
			SQLExceptionTranslator sqlExceptionTranslator); @Override DataAccessException translateExceptionIfPossible(RuntimeException ex);  }