在PLSQL Oracle中抛出特定的错误消息...在hibernate中捕获?

Egg*_*Egg 3 java oracle plsql stored-procedures hibernate

是否可以在PL/SQL oracle存储过程中抛出特定的错误消息,并在调用时在Hibernate中捕获它?

RC.*_*RC. 6

您可以从PL/SQL代码中抛出用户定义的错误消息.-20000至-20999之间的错误代码保留用于用户指定的错误消息.

您可以通过调用raise_application_errorPL/SQL中的函数来实现:

raise_application_error(-20001, 'Your error code message here');
Run Code Online (Sandbox Code Playgroud)

这将像普通的Oracle错误一样传播.

编辑:

我不是Hibernate的用户,但我在尝试寻找答案时发现了这一点,我认为它会引导您走上正确的道路.

try 
{
    // some hibernate calls
} 
catch (GenericJdbcException ge) 
{
    if(ge.getCause() != null && ge.getCause() instanceof SQLException) 
    {
        SQLException se = (SQLException)ge.getCause();

        // *****************************************************************
        // NOTE: THIS will be where you check for your customer error code.
        // *****************************************************************
        if(se.getErrorCode() == -20001) 
        {
            // your error handling for this case
        }
        else
        {
            throw ge; // do not swallow unhandled exceptions
        }
    }
    else
    {
        throw ge // do not swallow unhandled exceptions
    }
}
Run Code Online (Sandbox Code Playgroud)