Throwing specific error messages in PLSQL Oracle...catching in hibernate?
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?
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
}
}
you can use output parameter on the pl/sql and pick output in your code using ParameterMode.OUT
CREATE OR REPLACE procedure proc ( pls_out_put out number);
//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);
}
精彩评论