Hibernate和oracle存储过程
我有一个oracle存储过程:`Hibernate和oracle存储过程,oracle,hibernate,Oracle,Hibernate,我有一个oracle存储过程:` 我在oracle SQLDeveloper中对其进行了测试,效果良好: 游标=1(它找到它:=)) 现在,我尝试在java端与hybernate一起使用这个过程 映射: ` {调用getClientFromAuthentification(?,:login,:mdp)} ` 当我使用它时: ` ArrayList客户端; Query q=session.getNamedQuery(“getClientFromAuthentification_SP
我在oracle SQLDeveloper中对其进行了测试,效果良好:
游标=1(它找到它:=)) 现在,我尝试在java端与hybernate一起使用这个过程 映射:
`
{调用getClientFromAuthentification(?,:login,:mdp)}
`
当我使用它时:
`
ArrayList客户端;
Query q=session.getNamedQuery(“getClientFromAuthentification_SP”);
q、 setString(“登录”,“durant.jean”);
q、 setString(“mdp”、“durant.jean1234”);
clients=(ArrayList)q.list()`
我没有错误,但不幸的是列表是空的
请帮助我在存储过程中,使用
%NOTFOUND
而不是%ROWCOUNT
:
- 您应该仅将%rowcount用于 隐式游标 插入/更新/删除
- 您应该将%notfound与SELECTs一起使用 查看它们是否返回数据李>
create or replace procedure getClientFromAuthentification(myCursorResult out sys_refcursor, login in VARCHAR2, pwd in VARCHAR2) as
num_client NUMBER(6);
compte_epargne VARCHAR2(50);
compte_courant VARCHAR2(50);
nom VARCHAR2(50);
prenom VARCHAR2(50);
adresse VARCHAR2(100);
begin
open myCursorResult for
select client.num_client, client.num_compte_epargne, client.num_compte_courant,
client.nom_client, client.prenom_client, client.adresse_client
from client
where client.login_client = login and client.mdp_client = pwd;
fetch myCursorResult into num_client, compte_epargne, compte_courant, nom, prenom, adresse;
if myCursorResult%NOTFOUND then
raise NO_DATA_FOUND;
end if;
exception
when NO_DATA_FOUND then raise_application_error(-20001, 'pas de résultats');
end getClientFromAuthentification;
关于整个设计:
- 你为什么要退回用塑料袋包装的东西 光标正好有一行
- 为什么不将这些作为输出参数:
num_client,compte_epargne,compte_courant,nom,prenom,address
%ROWCOUNT
替换为%NOTFOUND
。让我来帮你,我把它添加到我的回答中。你如何用NOTFOUND引发“两行多行”异常。只需按NOTFOUND更改rowcount,hibernate就可以在我的对象列表中映射光标?如果你想引发“两行多行”异常,您可以尝试从游标中再提取一行,如果第二次提取尝试成功,则会引发“两行多行”(如果不是myCursorResult%NOTFOUND,则为)
`set serveroutput on;
declare
myCursor sys_refcursor;
begin
dbms_output.enable;
getclientfromauthentification(myCursor, 'durant.jean', 'durant.jean1234');
dbms_output.put_line('cursor = ' || myCursor%ROWCOUNT);
end;`
`<hibernate-mapping>
<class name="model.entity.Client">
<id name="num_client" type="int" />
<property name="num_compte_epargne" type="string" />
<property name="num_compte_courant" type="string" />
<property name="nom" type="string" />
<property name="prenom" type="string" />
<property name="adresse" type="string" />
</class>
<sql-query name="getClientFromAuthentification_SP" callable="true">
<return alias="client" class="model.entity.Client" >
<return-property name="num_client" column="NUM_CLIENT"/>
<return-property name="num_compte_epargne" column="NUM_COMPTE_EPARGNE"/>
<return-property name="num_compte_courant" column="NUM_COMPTE_CLIENT"/>
<return-property name="nom" column="NOM_CLIENT"/>
<return-property name="prenom" column="PRENOM_CLIENT"/>
<return-property name="adresse" column="ADRESSE_CLIENT"/>
</return>
{ call getClientFromAuthentification(?, :login, :mdp) }
</sql-query>
</hibernate-mapping>`
ArrayList<Client> clients;
Query q = session.getNamedQuery("getClientFromAuthentification_SP");
q.setString("login", "durant.jean");
q.setString("mdp", "durant.jean1234");
clients = (ArrayList<Client>) q.list();`
create or replace procedure getClientFromAuthentification(myCursorResult out sys_refcursor, login in VARCHAR2, pwd in VARCHAR2) as
num_client NUMBER(6);
compte_epargne VARCHAR2(50);
compte_courant VARCHAR2(50);
nom VARCHAR2(50);
prenom VARCHAR2(50);
adresse VARCHAR2(100);
begin
open myCursorResult for
select client.num_client, client.num_compte_epargne, client.num_compte_courant,
client.nom_client, client.prenom_client, client.adresse_client
from client
where client.login_client = login and client.mdp_client = pwd;
fetch myCursorResult into num_client, compte_epargne, compte_courant, nom, prenom, adresse;
if myCursorResult%NOTFOUND then
raise NO_DATA_FOUND;
end if;
exception
when NO_DATA_FOUND then raise_application_error(-20001, 'pas de résultats');
end getClientFromAuthentification;