1

你好。由于我似乎无法在我的 dao 中使用 spring DataAccessException 翻译机制,我想知道是否可以翻译

Internal Exception: java.sql.SQLException: [BEA][Oracle JDBC Driver][Oracle]ORA-00001: unique constraint (JSP_OWN.IDX_MC_CC_RAPPORTI_02) violated

手动到 DataAccessException 层次结构。

亲切的问候马西莫

4

1 回答 1

1

如果你有一个JdbcTemplate,你可以做

catch (SqlException e) {
  throw jdbcTemplate.getExceptionTranslator().translate("my task", null, e);
}

如果没有JdbcTemplate,直接看方法的源码JdbcTemplate.getExceptionTranslator()

public synchronized SQLExceptionTranslator getExceptionTranslator() {
    if (this.exceptionTranslator == null) {
        DataSource dataSource = getDataSource();
        if (dataSource != null) {
            this.exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dataSource);
        }
        else {
            this.exceptionTranslator = new SQLStateSQLExceptionTranslator();
        }
    }
    return this.exceptionTranslator;
}

并模仿它的行为:-)

于 2012-01-11T09:11:27.093 回答