日期:2014-05-16  浏览次数:20477 次

hibernate 调用存储过程返回列表

1、使用存储过程

???? (1)建立程序包

?

CREATE   OR   REPLACE   PACKAGE   TESTPACKAGE     AS

  TYPE   Test_CURSOR   IS   REF   CURSOR;

end   TESTPACKAGE; 

??? (2)建立存储过程

CREATE   OR   REPLACE   PROCEDURE   TESTC(p_CURSOR   out   TESTPACKAGE.Test_CURSOR)   IS  
BEGIN
        OPEN   p_CURSOR   FOR   SELECT   *   FROM   testTable;
END   TESTC; 

?

2、使用FUNCTION

CREATE OR REPLACE FUNCTION  seletall
    RETURN SYS_REFCURSOR
AS
   test_cur SYS_REFCURSOR;
BEGIN
    OPEN test_cur for 
    select * from emp;

     return test_cur;
END;

?

?

3、hibernate配置

    <class name="Emp" table="emp">  
            <id name="id" column="id">  
                <generator class="native"/>  
            </id>  
            <property name="name" column="name" type="string" />  
            <property name="age" column="age" type="integer" />  
    </class>  
       <sql-query name="getEmp" callable="true">  
         <return alias="emp" class="Emp">  
         <return-property name="id" column="id" />  
         <return-property name="name" column="name" />  
         <return-property name="age" column="age" />  
         </return>  
         {call proc()}  
       </sql-query>  

?

?

4、JAVA代码

?

    Session ss= HibernateSessionFactory.getSession()  
    List li=ss.getNamedQuery("getEmp").list();  
    ss.close();  
?