下面列出了怎么用org.hibernate.exception.spi.SQLExceptionConverter的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public SQLExceptionConverter buildSQLExceptionConverter() {
return new SQLExceptionConverter() {
@Override
public JDBCException convert(SQLException sqlException,
String message, String sql) {
final String sqlState = JdbcExceptionHelper
.extractSqlState(sqlException);
if (sqlState != null) {
if (SQL_GRAMMAR_CATEGORIES.contains(sqlState)) {
return new SQLGrammarException(message, sqlException,
sql);
} else if (DATA_CATEGORIES.contains(sqlState)) {
return new DataException(message, sqlException, sql);
} else if (LOCK_ACQUISITION_CATEGORIES.contains(sqlState)) {
return new LockAcquisitionException(message,
sqlException, sql);
}
}
return null;
}
};
}
@Override
public SQLExceptionConverter buildSQLExceptionConverter() {
return new SQLExceptionConverter() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
final int errorCode = sqlException.getErrorCode();
if (errorCode == SQLITE_CONSTRAINT) {
final String constraintName = EXTRACTER.extractConstraintName(sqlException);
return new ConstraintViolationException(message, sqlException, sql, constraintName);
} else if (errorCode == SQLITE_TOOBIG || errorCode == SQLITE_MISMATCH) {
return new DataException(message, sqlException, sql);
} else if (errorCode == SQLITE_BUSY || errorCode == SQLITE_LOCKED) {
return new LockAcquisitionException(message, sqlException, sql);
} else if ((errorCode >= SQLITE_IOERR && errorCode <= SQLITE_PROTOCOL) || errorCode == SQLITE_NOTADB) {
return new JDBCConnectionException(message, sqlException, sql);
}
return new GenericJDBCException(message, sqlException, sql);
}
};
}
@Override
public SQLExceptionConverter buildSQLExceptionConverter() {
return new SQLExceptionConverter() {
@Override
public JDBCException convert(SQLException sqlException,
String message, String sql) {
final String sqlState = JdbcExceptionHelper
.extractSqlState(sqlException);
if (sqlState != null) {
if (SQL_GRAMMAR_CATEGORIES.contains(sqlState)) {
return new SQLGrammarException(message, sqlException,
sql);
} else if (DATA_CATEGORIES.contains(sqlState)) {
return new DataException(message, sqlException, sql);
} else if (LOCK_ACQUISITION_CATEGORIES.contains(sqlState)) {
return new LockAcquisitionException(message,
sqlException, sql);
}
}
return null;
}
};
}
protected SQLExceptionConverter getSQLExceptionConverter() {
return getSQLExceptionHelper().getSqlExceptionConverter();
}
@Override
public SQLExceptionConverter getSQLExceptionConverter() {
return delegate.getSQLExceptionConverter();
}
public SQLExceptionConverter getSQLExceptionConverter() {
return sessionFactoryImplementor.getSQLExceptionConverter();
}
@Override
@Deprecated
public SQLExceptionConverter buildSQLExceptionConverter() {
return getInstance().buildSQLExceptionConverter();
}
/**
* Create an exception helper with a specific exception converter.
*
* @param sqlExceptionConverter The exception converter to use.
*/
public SqlExceptionHelper(SQLExceptionConverter sqlExceptionConverter, boolean logWarnings) {
this.sqlExceptionConverter = sqlExceptionConverter;
this.logWarnings = logWarnings;
}
/**
* Access the current exception converter being used internally.
*
* @return The current exception converter.
*/
public SQLExceptionConverter getSqlExceptionConverter() {
return sqlExceptionConverter;
}
/**
* Inject the exception converter to use.
* <p/>
* NOTE : <tt>null</tt> is allowed and signifies to use the default.
*
* @param sqlExceptionConverter The converter to use.
*/
public void setSqlExceptionConverter(SQLExceptionConverter sqlExceptionConverter) {
this.sqlExceptionConverter = ( sqlExceptionConverter == null ? DEFAULT_CONVERTER : sqlExceptionConverter );
}
/**
* Retrieves the SQLExceptionConverter in effect for this SessionFactory.
*
* @return The SQLExceptionConverter for this SessionFactory.
*
* @deprecated since 5.0; use {@link JdbcServices#getSqlExceptionHelper()} ->
* {@link SqlExceptionHelper#getSqlExceptionConverter()} instead as obtained from {@link #getServiceRegistry()}
*/
@Deprecated
default SQLExceptionConverter getSQLExceptionConverter() {
return getServiceRegistry().getService( JdbcServices.class ).getSqlExceptionHelper().getSqlExceptionConverter();
}
/**
* Build an instance of the SQLExceptionConverter preferred by this dialect for
* converting SQLExceptions into Hibernate's JDBCException hierarchy.
* <p/>
* The preferred method is to not override this method; if possible,
* {@link #buildSQLExceptionConversionDelegate()} should be overridden
* instead.
*
* If this method is not overridden, the default SQLExceptionConverter
* implementation executes 3 SQLException converter delegates:
* <ol>
* <li>a "static" delegate based on the JDBC 4 defined SQLException hierarchy;</li>
* <li>the vendor-specific delegate returned by {@link #buildSQLExceptionConversionDelegate()};
* (it is strongly recommended that specific Dialect implementations
* override {@link #buildSQLExceptionConversionDelegate()})</li>
* <li>a delegate that interprets SQLState codes for either X/Open or SQL-2003 codes,
* depending on java.sql.DatabaseMetaData#getSQLStateType</li>
* </ol>
* <p/>
* If this method is overridden, it is strongly recommended that the
* returned {@link SQLExceptionConverter} interpret SQL errors based on
* vendor-specific error codes rather than the SQLState since the
* interpretation is more accurate when using vendor-specific ErrorCodes.
*
* @return The Dialect's preferred SQLExceptionConverter, or null to
* indicate that the default {@link SQLExceptionConverter} should be used.
*
* @see {@link #buildSQLExceptionConversionDelegate()}
* @deprecated {@link #buildSQLExceptionConversionDelegate()} should be
* overridden instead.
*/
@Deprecated
public SQLExceptionConverter buildSQLExceptionConverter() {
return null;
}