ITeye博客

使用HibernateTemplate调用存储过程

pingwei000 · 2010-06-07 14:33 · 企业架构 · Markdown

共有两种方式 第一种



CallableStatement cstmt =

hibernateTemplate.getSessionFactory

().getCurrentSession().connection

().prepareCall("{call proc_name}");

cstmt.executeUpdate();



第二种



hibernateTemplate.execute( new HibernateCallback() { public Object doInHibernate

(Session session) throws

SQLException { CallableStatement cs =

session.connection().prepareCall

("{call proc_name}");

cs.executeUpdate(); return null; } } );

hibernate3.0以上提供了两种方式执行存储过程。

第一种 :用JDBC 方式

Session session =HibernateSessionFactory.getSession(); //获取hibernate会话

Connection conn = session.connection(); // 用session对象获取连接

ResultSet rs =null;

try { CallableStatement call = conn.prepareCall("{Call pro_getManager(?,?)}");

call.setString(1, "admin"); call.setString(2, "admin"); rs = call.executeQuery();

} catch (Exception e) { e.printStackTrace(); }finally {

rs.close();//关闭 session.close();//关闭连接 HibernateSessionFactory.closeSession(); //关闭会话 }

这种方式本人感觉与hibernate结合不很紧密,还难以控制,还要自己控制关闭

第二种: 用hibernate中SQLQuery 接口执行,其实与执行sql没有两样。

Session session =HibernateSessionFactory.getSession(); //获取hibernate会话

String procName="{Call pro_getManager(?,?) }";

SQLQuery query = session.createSQLQuery(proc); query.setString(0, "admin"); query.setString(1, "admin"); List list =query.list();

session.close();

HibernateSessionFactory.closeSession();

SQLQuery 接口功能很强大吧

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。