2

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

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 o'clock EET> <02-01-2013 10:04:27 o'clock EET> <10:04:27,811 ERROR [IncomingPaperworkMBean] EJB 异常:; 嵌套异常是:org.hibernate.exception.SQLGrammarException:无法执行查询;嵌套异常是:org.hibernate.exception.SQLGrammarException:无法执行查询>

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

4

1 回答 1

0

使用 oracle,我使用查询语法

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

它对我有用。

于 2013-01-02T09:20:55.420 回答