简体   繁体   中英

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.

I am calling webservice from my app that is using the stored procedure to return data in a hierarchical format. 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,

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
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM