日期:2014-05-19  浏览次数:20765 次

hibernate 中调用存储过程的问题
Java code

CallableStatement callstate;
        ResultSet rs;
        List<Account> list = new ArrayList<Account>();
        try {
callstate = factory.getCurrentSession().connection().prepareCall(
                    "call p_zhou.quer_aaccount(?,?)");
            callstate.setString(1, "2");
            callstate.registerOutParameter(2, oracle.jdbc.OracleTypes.CURSOR);
            callstate.execute();
            rs = (ResultSet) callstate.getObject(2);
            int i = rs.getMetaData().getColumnCount();// 得到列数
            while (rs.next()) {
                Account account = new Account();
                account.setName(rs.getString(1));
                account.setPassword(rs.getString(2));
                list.add(account);
            }


这是目前采用的方法 jdbc方式的,但是由Session获得Connection是过期的!目前知道还有种配置文件的方式,但不知道该如何写,我的存储过程传入一个参数返回一个游标!
SQL code

create or replace package body p_zhou is

  -- Private type declarations
  -- Private constant declarations
  -- Private variable declarations
  -- Function and procedure implementations
  procedure quer_aaccount(zpassword varchar2, Zresult out zjcursor) as
  begin
    open Zresult for
      select t.* from account t where t.password = zpassword;
  exception
    when others then
      p_Myerr.Handle(sqlcode, sqlerrm);
  end;

end p_zhou;



------解决方案--------------------
你可以Session session =HibernateSessionFactory.getSession();
SQLQuery query = session.createSQLQuery("{Call p_zhou.quer_aaccount(?,?)}");
------解决方案--------------------
或者session.doWork(new Work() {

@Override
public void execute(Connection connection) throws SQLException {
connection.prepareCall("");
}
});
------解决方案--------------------
connection.prepareCall("");