简体   繁体   中英

using stored procedure in hibernate 3

您好,自5个月以来,我一直在使用hibernate + spring,但从未在hibernate中使用过存储过程,因此,任何人都可以告诉我如何从DB(MySQL)调用存储过程。

Hibernate defines stored procedure calls as a named query. The docs explain how to set this up in the Hibernate config.

From Spring, you can call a named query using the various HibernateTemplate.findByNamedQuery(...) methods.

Spring has an StoredProcedure class that you can extend to call stored procedures.

class MyStoredProcedure extends StoredProcedure {
     public MyStoredProcedure(DataSource ds) {
          this.setDataSource(ds);
          this.setSql("store_procedure_name");
          this.declareParameter(new SqlParameter("name", Types.VARCHAR);
          this.compile();
     }

     public void callProcedure() {
          Map<string, String> inParams = new HashMap<String, String>();
          inParams.put("name", "taher");
          try {
               execute(inParams);
          } catch (DataAccessException dae) {
          }
     }
}

Since you are already using Spring with Hibernate I would suggest using Spring classes. You can extend the StoredProcedure class mentioned above, there are other alternatives as well. If you have a basic stored procedure I would say the easiest way to call it would be to use Spring's SimpleJdbcCall class. The Spring documentation covers this class quite nicely with code snippets.

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