下面列出了怎么用org.hibernate.exception.DataException的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
final int errorCode = JdbcExceptionHelper.extractErrorCode(sqlException) & 0xFF;
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);
}
// returning null allows other delegates to operate
return null;
}
};
}
/**
* Convert the given SQLException into Hibernate's JDBCException hierarchy.
*
* @param sqlException The SQLException to be converted.
* @param message An optional error message.
* @param sql Optionally, the sql being performed when the exception occurred.
* @return The resulting JDBCException; returns null if it could not be converted.
*/
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
String sqlStateClassCode = JdbcExceptionHelper.extractSqlStateClassCode( sqlException );
if ( sqlStateClassCode != null ) {
Integer errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
if ( INTEGRITY_VIOLATION_CATEGORIES.contains( errorCode ) ) {
String constraintName =
getConversionContext()
.getViolatedConstraintNameExtracter()
.extractConstraintName( sqlException );
return new ConstraintViolationException( message, sqlException, sql, constraintName );
}
else if ( DATA_CATEGORIES.contains( sqlStateClassCode ) ) {
return new DataException( message, sqlException, sql );
}
}
return null; // allow other delegates the chance to look
}
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return new SQLExceptionConversionDelegate() {
@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 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 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 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;
}
};
}
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return new SQLExceptionConversionDelegate() {
@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 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 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);
}
};
}
public static String extractExceptionMessage(Exception e) {
if (e instanceof UndeclaredThrowableException) {
Throwable cause = ((UndeclaredThrowableException) e).getUndeclaredThrowable().getCause();
if (cause instanceof PersistenceException) {
cause = ((PersistenceException) cause).getCause();
if (cause instanceof DataException) {
return ((DataException) cause).getSQLException().getMessage();
}else if(cause instanceof SQLGrammarException) {
return ((SQLGrammarException) cause).getSQLException().getMessage();
}
}
}
return e.getMessage();
}
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
if ( SQLClientInfoException.class.isInstance( sqlException )
|| SQLInvalidAuthorizationSpecException.class.isInstance( sqlException )
|| SQLNonTransientConnectionException.class.isInstance( sqlException )
|| SQLTransientConnectionException.class.isInstance( sqlException ) ) {
return new JDBCConnectionException( message, sqlException, sql );
}
else if ( DataTruncation.class.isInstance( sqlException ) ||
SQLDataException.class.isInstance( sqlException ) ) {
throw new DataException( message, sqlException, sql );
}
else if ( SQLIntegrityConstraintViolationException.class.isInstance( sqlException ) ) {
return new ConstraintViolationException(
message,
sqlException,
sql,
getConversionContext().getViolatedConstraintNameExtracter().extractConstraintName( sqlException )
);
}
else if ( SQLSyntaxErrorException.class.isInstance( sqlException ) ) {
return new SQLGrammarException( message, sqlException, sql );
}
else if ( SQLTimeoutException.class.isInstance( sqlException ) ) {
return new QueryTimeoutException( message, sqlException, sql );
}
else if ( SQLTransactionRollbackException.class.isInstance( sqlException ) ) {
// Not 100% sure this is completely accurate. The JavaDocs for SQLTransactionRollbackException state that
// it indicates sql states starting with '40' and that those usually indicate that:
// <quote>
// the current statement was automatically rolled back by the database because of deadlock or
// other transaction serialization failures.
// </quote>
return new LockAcquisitionException( message, sqlException, sql );
}
return null; // allow other delegates the chance to look
}
@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;
}
};
}
/**
* Adapt the external css.
*/
private void adaptExternalCss() {
for (Element el : externalCssElements) {
List<CSSMediaQuery> mediaList = getListOfMediaFromAttributeValue(el);
String resourcePath = el.attr("abs:href");
getExternalResourceAndAdapt(resourcePath, mediaList);
}
Set<Long> relatedCssIdSet = new HashSet<>();
// At the end of the document we link each external css that are
// already fetched and that have been encountered in the SSP to the SSP.
LOGGER.debug("Found " + relatedExternalCssSet.size() +
" external css in "+ getSSP().getURI());
for (StylesheetContent cssContent : relatedExternalCssSet) {
if (cssContent.getAdaptedContent() == null) {
cssContent.setAdaptedContent(CSS_ON_ERROR);
}
LOGGER.debug("Create relation between "+getSSP().getURI() +
" and " + cssContent.getURI());
// to avoid fatal error when persist weird sourceCode
try {
// the content is saved only when the id is null which means
// that the content hasn't been persisted yet. Otherwise, the
// save is uneeded and the id is used to create the relation
// with the current SSP
if (cssContent.getId() == null) {
cssContent = (StylesheetContent)getContentDataService().saveOrUpdate(cssContent);
}
relatedCssIdSet.add(cssContent.getId());
} catch (PersistenceException | DataException pe) {
adaptedContentOnError(cssContent, relatedCssIdSet);
}
}
getContentDataService().saveContentRelationShip(getSSP(), relatedCssIdSet);
}
@Test
public void givenQueryWithDataTypeMismatch_WhenQueryExecuted_thenDataException() {
thrown.expectCause(isA(DataException.class));
thrown.expectMessage(
"org.hibernate.exception.DataException: could not prepare statement");
Session session = sessionFactory.openSession();
NativeQuery<Product> query = session.createNativeQuery(
"select * from PRODUCT where id='wrongTypeId'", Product.class);
query.getResultList();
}
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
if ( sqlState != null ) {
String sqlStateClassCode = JdbcExceptionHelper.determineSqlStateClassCode( sqlState );
if ( sqlStateClassCode != null ) {
if ( SQL_GRAMMAR_CATEGORIES.contains( sqlStateClassCode ) ) {
return new SQLGrammarException( message, sqlException, sql );
}
else if ( INTEGRITY_VIOLATION_CATEGORIES.contains( sqlStateClassCode ) ) {
final String constraintName = getConversionContext()
.getViolatedConstraintNameExtracter()
.extractConstraintName( sqlException );
return new ConstraintViolationException( message, sqlException, sql, constraintName );
}
else if ( CONNECTION_CATEGORIES.contains( sqlStateClassCode ) ) {
return new JDBCConnectionException( message, sqlException, sql );
}
else if ( DATA_CATEGORIES.contains( sqlStateClassCode ) ) {
return new DataException( message, sqlException, sql );
}
}
if ( "40001".equals( sqlState ) ) {
return new LockAcquisitionException( message, sqlException, sql );
}
if ( "40XL1".equals( sqlState ) || "40XL2".equals( sqlState )) {
// Derby "A lock could not be obtained within the time requested."
return new PessimisticLockException( message, sqlException, sql );
}
// MySQL Query execution was interrupted
if ( "70100".equals( sqlState ) ||
// Oracle user requested cancel of current operation
( "72000".equals( sqlState ) && errorCode == 1013 ) ) {
throw new QueryTimeoutException( message, sqlException, sql );
}
}
return null;
}