[英]Stored procedure using hibernate
I want to use stored procedure to add and delete hierarchical data using hibernate.我想使用存储过程来添加和删除使用休眠的分层数据。 For doing that I want to first check if that procedure exist in database and if not create it.为此,我想首先检查该过程是否存在于数据库中,如果不存在,则创建它。 I looked for the standard way of doing this in hibernate but found nothing.我寻找在休眠中执行此操作的标准方法,但一无所获。 I am just curious to know that is it good to create a procedure using hibernate or is there a better way of doing operation on hierarchical data in hibernate.我只是想知道使用 hibernate 创建过程是否好,或者是否有更好的方法在 hibernate 中对分层数据进行操作。
I am calling webservice from my app that is using the stored procedure to return data in a hierarchical format.我正在从我的应用程序调用 webservice,该应用程序使用存储过程以分层格式返回数据。 If the procedure is deleted at runtime unknowingly, my app will never be able to retrieve data.如果该过程在运行时不知不觉中被删除,我的应用程序将永远无法检索数据。 So what I want if procedure does not exist create it.所以如果程序不存在,我想要什么创建它。 I am not sure is it the right way or not?我不确定这是否正确? I need guidance...我需要指导...
You can call stored function or procedure in Hibernate as follow,您可以按如下方式在 Hibernate 中调用存储的函数或过程,
session.doWork(new Work() {
public void execute(Connection connection) throws SQLException {
CallableStatement call = connection.prepareCall("{ ? = call MYSCHEMA.MYFUNC(?,?) }");
call.registerOutParameter( 1, Types.INTEGER ); // or whatever it is
call.setLong(2, id);
call.setLong(3, transId);
call.execute();
int result = call.getInt(1); // propagate this back to enclosing class
}
});
Similarly相似地
int result = session.doReturningWork(new ReturningWork<Integer>() {
@Override
public Integer execute(Connection connection) throws SQLException {
CallableStatement call = connection.prepareCall("{ ? = call MYSCHEMA.MYFUNC(?,?) }");
call.registerOutParameter( 1, Types.INTEGER ); // or whatever it is
call.setLong(2, id);
call.setLong(3, transId);
call.execute();
return call.getInt(1); // propagate this back to enclosing class
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.