首页 > 解决方案 > Hibernate - 处理 SQLException

问题描述

全部,

我正在开发一个将休眠从 4.1.4.FINAL 升级到 5.2.17.FINAL 的项目。我们有一堆使用 org.hibernate.jdbc.Work 执行的 Sybase 存储过程。这些存储过程使用一些有效的错误代码(如 20010)引发错误。引发的错误消息被捕获并用于在 UI 上显示。这是引发错误的 Sybase 语法。

raiserror 20005 'Invalid'

我看到新版本的 hibernate 委托 SQL 异常转换为 JDBCException 层次结构中的特定异常。看 -

org.hibernate.exception.internal.StandardSQLExceptionConverter

如果它没有找到特定的异常,那么它会创建带有默认消息集的 GenericJDBCException。例如见

org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.coordinateWork - 
Here the SQL exception is caught and the convert method is called with message 'error executing work'. So genericJDBCException.getMessage() will give this message.

我知道 GenericJDBCException.getSQLException().getMessage() 会给出实际的 sql 异常消息。但是改变现有的代码是不可行的。

有没有办法添加我们自己的委托,以便我可以检查错误代码并返回带有 SQLException 消息的异常。或者有没有更好的方法来处理这个?

谢谢

标签: javahibernate

解决方案


推荐阅读