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?
views:
249answers:
1
+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
2009-10-09 14:29:05
how do I catch that in hibernate?
Egg
2009-10-09 14:52:50
`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
2009-10-09 15:30:07
nice Job RC, that should be enuogh
Egg
2009-10-09 15:37:24