Throwing specific error messages in PLSQL Oracle ... catching hibernation?

Is it possible to send a specific error message to an oracle PL / SQL stored procedure and catch it in Hibernate when it is called?

+2


source to share


2 answers


You can submit custom error messages from PL / SQL code. Error codes -20000 to -20999 are reserved for user-specified error messages.

You do this by calling a function raise_application_error

in your PL / SQL:

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

      

This will propagate just like normal Oracle bugs.



Edit:

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

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
    }
}

      

+5


source


you can use an output parameter on pl / sql and select the output in your code with ParameterMode.OUT

CREATE OR REPLACE procedure proc (number pls_out_put out);

// call output



 query.registerStoredProcedureParameter( "pls_out_put", Integer.class, ParameterMode.OUT);`

      

int result = (Integer) query.getOutputParameterValue ("pls_out_put");

    if (result==1){
     message = new FacesMessage(FacesMessage.SEVERITY_INFO, "user defined message", "user defined message");

     FacesContext.getCurrentInstance().addMessage(null, message);
    } else if(result==0) {
         message = new FacesMessage(FacesMessage.SEVERITY_INFO, "User defined message", "user defined message");

         FacesContext.getCurrentInstance().addMessage(null, message);
    }

      

0


source







All Articles