使用Spring Data JPA正确调用存储过程

时间:2017-03-01 12:38:09

标签: java oracle stored-procedures spring-data-jpa

我在尝试从Spring数据存储库调用存储过程时遇到了麻烦。按照Spring数据文档和SO上的几个答案,这似乎是正确的方法,但我一直有这个错误:

PLS-00306: wrong number or types of arguments in call to 'GET_DESCR_BDD_BDS'

这是存储过程签名

procedure GET_DESCR_BDD_BDS(PRGPVV in number,
                                COD_SEZ in number,
                                FL_BDD_BDS in number,
                                prg_doc out varchar2,
                                repo_pos out number
                                )

这就是我实施电话的方式(我可能在不同的尝试中搞砸了一些东西) 模型

@NamedStoredProcedureQuery(name = "DescrBddBds.descr", 
    procedureName = "PRK_BDD.GET_DESCR_BDD_BDS",
    parameters = {
        @StoredProcedureParameter(mode = ParameterMode.IN, name = "PRGPVV", type = Integer.class),
        @StoredProcedureParameter(mode = ParameterMode.IN, name = "COD_SEZ", type = Integer.class),
        @StoredProcedureParameter(mode = ParameterMode.IN, name = "FL_BDD_BDS", type = Integer.class)
        ,
        @StoredProcedureParameter(mode = ParameterMode.OUT, name = "prg_doc", type = String.class),
        @StoredProcedureParameter(mode = ParameterMode.OUT, name = "repo_pos", type = Integer.class)
    },
    resultClasses = DescrBddBds.class
)
@Entity
public class DescrBddBds implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = -2182033603838684233L;
    @Id
    @Column(name = "prg_doc")
    private String prgDoc;
    @Column(name = "repo_pos")
    private Integer repoPos;

    public String getPrgDoc() {
        return prgDoc;
    }
    public void setPrgDoc(String prgDoc) {
        this.prgDoc = prgDoc;
    }
    public Integer getRepoPos() {
        return repoPos;
    }
    public void setPepoPos(Integer repoPos) {
        this.repoPos = repoPos;
    }

}

存储库

@Repository
public interface HtmlProceduresRepo extends CrudRepository<DescrBddBds, String> {

    @Procedure(name = "descr", procedureName="PRK_BDD.GET_DESCR_BDD_BDS")
    DescrBddBds descr(@Param("PRGPVV") Integer codiceDoc, @Param("COD_SEZ") Integer sezione, @Param("FL_BDD_BDS") Integer flagBddBds);
}

使用相同的用户从SQL Developer调用该过程,我从应用程序调用它可以正常工作

var b number;
var d number;
var e number;
exec :b:= 1;
exec :d:= 2;
exec :e:= 3;
execute PRK_BDD.GET_DESCR_BDD_BDS(:b, :d, :e, :out_param1, :out_param2);
print out_param1;
print out_param2;

2 个答案:

答案 0 :(得分:1)

最后我发现Spring Data JPA目前不支持具有多个输出参数的存储过程。这是该项目的一个悬而未决的问题,自从2年前开放以来,它似乎没有任何进展

https://github.com/spring-projects/spring-data-examples/issues/80

https://jira.spring.io/browse/DATAJPA-707

https://jira.spring.io/browse/DATAJPA-748

答案 1 :(得分:1)

具有多个OUT参数see的更新解决方案。

@NamedStoredProcedureQueries({ //
        @NamedStoredProcedureQuery(name = "User.plus1", procedureName = "plus1inout",
                parameters = { @StoredProcedureParameter(mode = ParameterMode.IN, name = "arg", type = Integer.class),
                        @StoredProcedureParameter(mode = ParameterMode.OUT, name = "res", type = Integer.class) }), //
        @NamedStoredProcedureQuery(name = "User.plus1IO2", procedureName = "plus1inout2",
                parameters = { @StoredProcedureParameter(mode = ParameterMode.IN, name = "arg", type = Integer.class),
                        @StoredProcedureParameter(mode = ParameterMode.OUT, name = "res", type = Integer.class),
                        @StoredProcedureParameter(mode = ParameterMode.OUT, name = "res2", type = Integer.class) }), //
        @NamedStoredProcedureQuery(name = "User.plus1IOoptional", procedureName = "plus1inoutoptional",
                parameters = { @StoredProcedureParameter(mode = ParameterMode.IN, name = "arg", type = Integer.class),
                        @StoredProcedureParameter(mode = ParameterMode.OUT, name = "res", type = Integer.class),
                        @StoredProcedureParameter(mode = ParameterMode.OUT, name = "res2", type = Integer.class) }) // DATAJPA-1579
})