我尝试使用Hibernate 4.3.11.Final调用一个在oracle 11g中返回整数的函数,当我尝试调用它时,它会抛出此异常:
ORA-14551: cannot perform a DML operation inside a query
这是命名查询:
@NamedNativeQuery(name = "generaFolio",
query = " { CALL GENERA_FOLIO( :idArea ) } ")
在函数内部有一个CURSOR和一个UPDATE语句
功能如下:
create or replace FUNCTION GENERA_FOLIO
(
areaorigen number
) return number
as
v_FOLIO number;
resource_busy EXCEPTION;
timeout_expired EXCEPTION;
PRAGMA EXCEPTION_INIT(timeout_expired,-30006);
PRAGMA EXCEPTION_INIT (r
esource_busy, -54);
cursor c1 is
select FOLIO FROM FOLIOS f1
WHERE
folio = (select min(folio) from Folios f2 where f2.IDAREA = areaorigen and vlock='D')
and f1.idarea = areaorigen
and vlock='D'
for update nowait;
BEGIN
-- DBMS_OUTPUT.put_line('Folio para area :: ' || areaorigen);
BEGIN
open c1;
fetch c1 into v_FOLIO;
close c1;
EXCEPTION
When no_data_found then
RAISE_APPLICATION_ERROR (-20001, 'No Existen datos en Folio-area : ' || areaorigen);
When timeout_expired then
RAISE_APPLICATION_ERROR (-20002, 'Tiempo de Espera consumido para area : ' || areaorigen);
When resource_busy then
RAISE_APPLICATION_ERROR (-20003, 'Folio bloqueado, para la area : ' || areaorigen);
When others then
RAISE_APPLICATION_ERROR (-20004, 'Fallo en el momento de tomar folio, de la sig. area: ' ||
areaorigen || ' >> Error number ' || SQLCODE || ' >> ' || SQLERRM);
end;
--DBMS_OUTPUT.put_line('Folio a Bloquear :: ' || v_folio);
UPDATE folios SET vlock = 'A'
WHERE IDAREA = areaorigen
and folio = v_FOLIO;
--DBMS_OUTPUT.put_line('Folio a Bloqueado! ');
return v_FOLIO;
end GENERA_FOLIO;
我执行它的方式是:
Query query = getSession().getNamedQuery(queryName);
setParameters(params, query);
Object uniqueResult = query.uniqueResult();
调用另一个返回varchar2并且只有SELECTs的函数才能正常工作,
请帮助:(
注1: 将查询声明为:
@NamedNativeQuery(name = "generaFolio", //
query = " { ? = CALL GENERA_FOLIO( :idArea ) } ") })
它抛出了这个错误:
org.hibernate.QueryException: Expected positional parameter count: 1, actual parameters: [] [{ ? = CALL GENERA_FOLIO( :idArea ) }]
因为,据我所知,您可以按位置("?")或标识符(":idArea")定义参数,我只需设置一个: /
注2:
也尝试了这个:
How to call Oracle Function or Procedure using Hibernate 4 (EntityManager) or JPA 2
喜欢:
@NamedNativeQuery(name = "generaFolio", //
query = " { ? = CALL GENERA_FOLIO( :idArea ) } ", //
hints = { @QueryHint(name = "org.hibernate.callable", value = "true") }),
@NamedNativeQuery(name = "generaFolio2", //
query = "{ call GENERA_FOLIO(?,:idArea) }", //
hints = { @QueryHint(name = "org.hibernate.callable", value = "true") })
但在:
Object uniqueResult = query.getFirstResult();
都返回NULL ...
答案 0 :(得分:0)
通过将此PRAGMA添加到Oracle函数来避免ORA-14551错误:
pragma autonomous_transaction;
最好通过修改Hibernate / Java来解决这个问题,以便在不同的上下文中调用该函数,但我不知道该怎么做。