Hibernate HQL基础 调用数据库存储过程

在Hibernate中也能够经过SQLQuery对象调用数据库的存储过程,可是要求存储过程必须返回一个结果集。 如在Oracle数据库的一个存储过程为: CREATE OR REPLACE PROCEDURE selectGuestbookById (sp_ref OUT SYS_REFCURSOR,inputId IN guestbook.id%type)AS BEGIN  OPEN sp_ref FOR    select *from guestbook where id=inputId; END selectGuestbookById; 调用的过程以下: 要经过Hibernate调用selectGuestbookById存储过程,还须要在Guestbook.hbm.xml文件中为其命名 <sql-query name = “com.kkoolerter.beans.Guestbook.getGuestbookBySP” callable="true">   <return alias="Guestbook" class = "com.kkoolerter.beans.Guestbook" />     {call selectGuestbookById(?:inputId)} </sql-query> 标签<sql-query>定义一个存储过程,name属性设置其名称,callable="true"代表这是一个存储过程<return>子标签设置返回记录封装的对象类型,{call selectGuestbookById(/:inputId)}是调用存储过程的代码。 调用存储过程的代码以下: Query query = session.getNamedQuery(“com.kkoolerter.beans.Guestbook.getGuestbookBySP”); query.setInteger("inputId",1); Guestbook gb = (Guestbook)session.uniqueryResult();
相关文章
相关标签/搜索