I have an oracle stored procedure :`
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%ROWCOUNT > 1 then raise TOO_MANY_ROWS;
else if myCursorResult%ROWCOUNT = 0 then raise NO_DATA_FOUND;
end if;
end if;
exception
when TOO_MANY_ROWS then raise_application_error(-20000, 'base corrompue');
when NO_DATA_FOUND then raise_application_error(-20001, 'pas de résultats');
end getClientFromAuthentification;`
I tested it in oracle sqldevelopper and it works fine :
`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;`
cursor =开发者_运维问答 1 (it found it :=))
Now I try to use this procedure on java side with hybernate.
The mapping :
`<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>`
When I use it :
`
ArrayList<Client> clients;
Query q = session.getNamedQuery("getClientFromAuthentification_SP");
q.setString("login", "durant.jean");
q.setString("mdp", "durant.jean1234");
clients = (ArrayList<Client>) q.list();`
I have no errors but Unfortunatly the list is empty...
Help me please
In the stored procedure, use %NOTFOUND
instead of %ROWCOUNT
:
- You should use %rowcount only with implicit cursors that do insert/update/delete.
- You should use %notfound with SELECTs to see if they return data or not. E.g.
edit: the whole proc added to the answer
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;
Regarding the whole design:
- Why do you return something wrapped in a cursor having exactly one row?
Why not have these as out parameter:
num_client, compte_epargne, compte_courant, nom, prenom, adresse
精彩评论