问题
I am calling Stored Proc from Spring Data JPA :
Procedure is:
create or replace procedure GET_LATEST_GC (arg1 IN VARCHAR2, res1 OUT VARCHAR2, res2 OUT VARCHAR2)
AS
BEGIN
DELETE FROM GC_T WHERE id = arg1;
COMMIT;
BEGIN
SELECT gc.NAME, s.SIP INTO res1, res2
FROM GC_T gc, STAFF_T s WHERE s.id = gc.id
AND START_TIME = (SELECT MAX(START_TIME) FROM GC_T);
EXCEPTION
WHEN others THEN
res1 := '';
END;
END;
Spring Data JPA code
//Repository
public interface ActiveDao extends JpaRepository<GcT,Integer> {
@Procedure(procedureName="GET_LATEST_GC")
Object[] plus1(@Param("arg1") String arg1);
}
//Entity
@Data
@Entity
@NamedStoredProcedureQuery(name = "GET_LATEST_GC",
procedureName = "GET_LATEST_GC", parameters = {
@StoredProcedureParameter(mode = ParameterMode.IN, name = "arg1", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "res1", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "res2", type = String.class)})
@Table(schema = "abc", name = "GC_T")
public class GcT implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
private String id;
@Column(name = "NAME")
private String name;
}
//Call
Object[] activeGCInfo =activeDao.plus1(arg);
Procedure is accepting one parameter and I am also passing 1 argument.Then also I am getting this error:
Hibernate: {call GET_LATEST_GC(?,?)} ERROR o.h.e.jdbc.spi.SqlExceptionHelper - ORA-06550: line 1, column 7:\nPLS-00306: wrong number or types of arguments in call to 'GET_LATEST_GC'\nORA-06550: line 1, column 7:\nPL/SQL: Statement ignored\n
Please let me know where I am doing wrong. Thank you
Update- Tried to add OUT params also as per suggestion
//Repo
public interface ActiveDao extends JpaRepository<GcT,Integer> {
@Procedure(procedureName="GET_LATEST_GC")
Object[] plus1(@Param("arg1") String arg1,@Param("res1") String res1,@Param("res2") String res2);
}
//Call
Object[] activeGCInfo =activeDao.plus1(arg,"","");
I am sending three args but it is showing me 4 args in error:
Hibernate: {call GET_LATEST_GC(?,?,?,?)} SqlExceptionHelper - ORA-06550: line 1, column 7:\nPLS-00306: wrong number or types of arguments in call to 'GET_LATEST_GC'\nORA-06550: line 1, column 7:\nPL/SQL: Statement ignored\n
回答1:
Here's what happened:
- you declared a procedure with 3 parameters: 1 in and 2 out
- you said: "Procedure is accepting one parameter and I am also passing 1 argument"
- that was the 1st procedure's parameter (
arg1 IN
) - it results in "PLS-00306: wrong number or types of arguments"
- that was the 1st procedure's parameter (
Of course it does; you need to provide 2 more arguments (datatype should be able to accept VARCHAR2
values returned by the procedure).
回答2:
Try changing the result from Object[] to Map<String, Object
, along with referencing the proc name with name
instead of procedureName. Based on the error, I'm not sure that it will fix it. Spring Data JPA does expect a Map as the return value for multiple output params, so each output param can be found as the key in that Map. But I think the main error is that procedureName maps directly to the db, but name=
will map to the correct Entity
//Repo
public interface ActiveDao extends JpaRepository<GcT,Integer> {
@Procedure(name="GET_LATEST_GC")
Map<String, Object> plus1(@Param("arg1") String arg1);
}
//Call
Map<String, Object> activeGCInfo =activeDao.plus1(arg);
来源:https://stackoverflow.com/questions/61454441/wrong-number-or-types-of-arguments-while-calling-stored-proc