使用hibernate调用存储过程

时间:2013-01-02 08:11:04

标签: hibernate stored-procedures

我在下面写了一个存储过程:

create or replace
PROCEDURE              SP_GET_NUMBER (p_cursor out sys_refcursor, departmentId in number, userId in number, documentTypeId in number)
as
regId integer;
sqlScript varchar2(60);
begin
sqlScript:='select registrationnumber_seq.nextval from dual';
execute immediate sqlScript into regid;
dbms_output.put_line('Nextval is: '||TO_CHAR(regid));
insert into roketsanuser.registrationnumbers
(id, departmentid, documenttypeid, registrationnumber, registrationstatus, status, updatedate, updateuserid, version)
values
(regId, null, null, regid, 0, 0, sysdate, 0, 0);
commit;
open p_cursor for
    select *
      from registrationnumbers reg
     where reg.id = regid;
end;

我在实体类的注释中定义:

@Entity(name = "RegistrationNumber")
@Table(name = "REGISTRATIONNUMBERS")
@SequenceGenerator(name = "REGISTRATIONNUMBER_SEQ", sequenceName = "REGISTRATIONNUMBER_SEQ")
@org.hibernate.annotations.NamedNativeQuery(name = "SP_GET_NUMBER", query = "{call ?= SP_GET_NUMBER(:departmentId, :userId, :documentTypeId)}",
                                            callable = true, readOnly = true, resultClass = RegistrationNumber.class)
@EntityListeners( { BaseEntityListener.class })
public class RegistrationNumber extends BaseEntity

我在我的Dao课程中调用查询:

public RegistrationNumber getNumberForIncomingPaperworkByStoredProcedure(EntityManager manager, RegistrationNumber registrationNumber)
{
    Session session = (Session)manager.getDelegate();
    org.hibernate.impl.SQLQueryImpl query = (SQLQueryImpl)session.getNamedQuery("SP_GET_NUMBER");

    query.setParameter("departmentId", ObjectUtil.isNotNull(registrationNumber.getDepartment()) ? registrationNumber.getDepartment().getId() : "");
    query.setParameter("userId", ObjectUtil.isNotNull(registrationNumber.getUser()) ? registrationNumber.getUser().getId() : "");
    query.setParameter("documentTypeId", ObjectUtil.isNotNull(registrationNumber.getDocumentType()) ? registrationNumber.getDocumentType().getId() : "");
    Collection list=query.list();
    return new RegistrationNumber();
}

但是当我执行查询发生的异常时: < 02-01-2013 10:04:24 EET> < 02-01-2013 10:04:27 EET> < 10:04:27,811 ERROR [IncomingPaperworkMBean] EJB异常:;嵌套异常是:     org.hibernate.exception.SQLGrammarException:无法执行查询;嵌套异常是:org.hibernate.exception.SQLGrammarException:无法执行查询>

可能是什么原因,请帮助我?

1 个答案:

答案 0 :(得分:0)

使用oracle我使用查询语法

query = "call SP_GET_NUMBER(?, :departmentId, :userId, :documentTypeId)", hints = {@QueryHint(name = "org.hibernate.callable", value = "true")}

它适用于我。