问题
Using Hibernate JPA to execute native queries in Oracle DB due to their complexity,
I want to catch an exception like " ORA-01722: Nombre non valide " thrown from SqlExceptionHelper class, but what was catching is:
class javax.persistence.PersistenceException: could not extract ResultSet
The logger error trace me that but not catched :
jdbc.spi.SqlExceptionHelper : ORA-01722: Nombre non valide
BigDecimal customerId = null;
try {
Query q = entityManager.createNativeQuery(
"select acc.account_id as customerId from Account ...");
customerId = (BigDecimal) q.getSingleResult();
} catch (Exception e) {
logger.info("CLASS : " + e.getClass());
if (e instanceof PersistenceException) { // should display ORA-01722: Nombre non valide ?
logger.info("ERRROR : " + e.getMessage());
throw new SQLException(e.getMessage());
}else
if (e instanceof SQLException) {
logger.info("ERRROR : " + e.getMessage());
throw new SQLException(e.getMessage());
}
logger.info("NOOOOOOOOOOOOO : " + e.getMessage());
throw new Exception(e.getMessage());
}
回答1:
SQLException is passed as an argument to SQLGrammarException constructor.
You can catch that, extract the root and display the native sql error or simply rethrow it:
} catch (SQLGrammarException e) {
logger.info("CLASS : " + e.getClass());
throw e.getSQLException();
} catch (Exception e){
...
}
来源:https://stackoverflow.com/questions/54323487/java-catch-and-display-sqlexceptionhelper