views:

249

answers:

1

Is it possible to throw a specific error message in a PL/SQL oracle stored procedure and catch it in Hibernate when it gets invoked?

+2  A: 

You can throw user-defined error messages from PL/SQL code. Error codes between -20000 until -20999 are reserved for user specified error messages.

You do so by calling the raise_application_error function within your PL/SQL:

raise_application_error(-20001, 'Your error code message here');

This will be propagated just like normal Oracle errors.

Edit:

I am not a user of Hibernate, but I found this while trying to find an answer and I think it will lead you down the right path.

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
    }
}
RC
how do I catch that in hibernate?
Egg
`org.hibernate.exception.GenericJdbcException` is an hibernate exception (https://www.hibernate.org/hib_docs/v3/api/org/hibernate/exception/GenericJDBCException.html). Just catch it.
Pascal Thivent
nice Job RC, that should be enuogh
Egg