Hibernate呼叫oracle儲存過程的問題

lljch發表於2007-05-26
有這樣一張表

create table student(id number,studentname varchar2(20)
<p class="indent">

我建了一個函式,根據輸入的學生名返回所有匹配的記錄

create or replace function getStudentByName(s in varchar2) return sys_refcursor
is
  c_cursor sys_refcursor;
begin
  open c_cursor for select id,studentname from student where name=s;
  return c_cursor;
end;

對映檔案是這樣的

<class name="ch.businessobjects.Student" table="student">
    <id name="id" type="int" column="id">
      <generator class="identity"/>
    </id>
    <property name="studentName" type="string" column="studentname"/>
</class>
<sql-query name="myquery">
    <return alias="s" class="ch.businessobjects.Student">
      <return-property name="id" column="a"/>
      <return-property name="studentName" column="b"/>
    </return>
    {?=call getStudentByName(?)}
 </sql-query>
<p class="indent">

java呼叫程式碼是這樣的

Query query = session.getNamedQuery("myquery");
query.setParameter(0, OracleTypes.CURSOR);//錯誤
query.setParameter(1, "張三");//錯誤
List l = query.list();
Iterator it = l.iterator();
while (it.hasNext())
{
   Student s = (Student) it.next();
   System.out.println(s.getId() + ":" + s.getStudentName());
}
<p class="indent">

程式老是在設定引數的地方報錯,請問在呼叫函式時到底該如何設定引數?

相關文章