Hibernate如何执行存储过程?
Posted 小甜瓜安东泥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate如何执行存储过程?相关的知识,希望对你有一定的参考价值。
Hibernate如何执行存储过程?
@Override
public Boolean setVarValue(final String processInstanceId, final String varName,
final String varValue) {
return getHibernateTemplate().execute(new HibernateCallback<Boolean>() {
@Override
public Boolean doInHibernate(Session session) throws HibernateException{
Connection conn = null;
CallableStatement callable = null;
boolean flag = false;
try {
conn = getConnection();
callable = conn.prepareCall("{CALL PROCESS_WARN.set_var_value(?,?,?)}");
callable.setString(1, processInstanceId);
callable.setString(2, varName);
callable.setString(3, varValue);
callable.execute();
flag = true;
} catch (Exception e) {
logger.error("execute procedure PROCESS_WARN.set_var_value mistakes !!!");
e.printStackTrace();
} finally {
try {
if (null != callable)
callable.close();
if (null != conn)
conn.close();
if (null != session)
session.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return flag;
}
});
}
、、、、、、、、、、、、、、、、
@Override
public Map<String, String> startProcess(final String warningFormId,
final String operationClassKey, final String cityOrgId) {
return getHibernateTemplate().execute(new HibernateCallback<Map<String,String>>() {
@Override
public Map<String,String> doInHibernate(Session session) throws HibernateException{
Connection conn = null;
CallableStatement callable = null;
Map<String,String> ret = new HashMap<String,String>();
try {
conn = getConnection();
callable = conn.prepareCall("{CALL PROCESS_WARN.START_PROCESS(?,?,?,?,?)}");
callable.setString(1, operationClassKey);
callable.setString(2, warningFormId);
callable.setString(3, cityOrgId);
callable.registerOutParameter(4, oracle.jdbc.OracleTypes.VARCHAR);
callable.registerOutParameter(5, oracle.jdbc.OracleTypes.VARCHAR);
callable.execute();
ret.put("process_instance_id", callable.getObject(4).toString());
ret.put("new_task_id_list", callable.getObject(5).toString());
} catch (Exception e) {
logger.error("execute procedure PROCESS_WARN.START_PROCESS mistakes !!!");
e.printStackTrace();
if(e.getMessage().indexOf("ORA-20012") != -1){
String [] strTemp = e.getMessage().split("#");
String msg = "未设置下一步骤的执行人!";
if(strTemp.length > 1){
msg = strTemp[1];
}
throw new NoAssigneeException(msg);
}
} finally {
try {
if (null != callable)
callable.close();
if (null != conn)
conn.close();
if (null != session)
session.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return ret;
}
});
}
以上是关于Hibernate如何执行存储过程?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用带有 setResultTransformer 和标量的 Hibernate 存储过程
如何使用 Hibernate con Sql Server 2000 调用存储过程?