在 PLSQL Oracle 中抛出特定的错误消息......在休眠中捕捉?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1544035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Throwing specific error messages in PLSQL Oracle...catching in hibernate?
提问by Egg
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?
是否可以在 PL/SQL oracle 存储过程中抛出特定的错误消息并在它被调用时在 Hibernate 中捕获它?
回答by RC.
You can throw user-defined error messages from PL/SQL code. Error codes between -20000 until -20999 are reserved for user specified error messages.
您可以从 PL/SQL 代码中抛出用户定义的错误消息。-20000 到 -20999 之间的错误代码保留用于用户指定的错误消息。
You do so by calling the raise_application_error
function within your PL/SQL:
您可以通过调用raise_application_error
PL/SQL 中的函数来实现:
raise_application_error(-20001, 'Your error code message here');
This will be propagated just like normal Oracle errors.
这将像正常的 Oracle 错误一样传播。
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.
我不是 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
}
}
回答by jaxtaposition
you can use output parameter on the pl/sql and pick output in your code using ParameterMode.OUT
您可以在 pl/sql 上使用输出参数并使用 ParameterMode.OUT 在代码中选择输出
CREATE OR REPLACE procedure proc ( pls_out_put out number);
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");
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);
}