Java Catch and Display SqlExceptionHelper

二次信任 提交于 2021-01-28 06:45:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!