我正在尝试创建一个调用存储过程的方法,以进行非常简单的插入和选择,
我在Oracle数据库上创建了一个存储过程,如下所示:
CREATE OR REPLACE PROCEDURE schema.insertNewTable(p_test IN VARCHAR2, p_out OUT VARCHAR2)
IS
BEGIN
INSERT INTO NEWTABLE("TEST") VALUES(p_test);
SELECT '1' INTO p_out FROM DUAL;
COMMIT;
END;
然后在实体上
@Procedure(procedureName = "insertNewTable", outputParameterName="p_out")
String insertNewTable(@Param("p_test") String p_test);
但是每次我调用此方法时,我都会得到:
2018-06-23 20:04:43,047|ORA-06550: row 1, colonna 7:
PLS-00306: wrong number or types of arguments in call to 'INSERTNEWTABLE'
ORA-06550: riga 1, column 7:
PL/SQL: Statement ignored
2018-06-23 20:04:43,100|org.springframework.dao.InvalidDataAccessResourceUsageException: Error calling CallableStatement.getMoreResults; SQL [insertNewTable]; nested exception is org.hibernate.exception.SQLGrammarException: Error calling CallableStatement.getMoreResults
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:261)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:503)
经过几次尝试(和例外),我修改了Entity和调用以避免“ OUT”类型,因此,我没有错误:
CREATE OR REPLACE PROCEDURE schema.insertNewTable(p_test IN VARCHAR2)
IS
BEGIN
INSERT INTO NEWTABLE("TEST") VALUES(p_test);
COMMIT;
END;
然后在实体上
@Procedure(procedureName = "insertNewTable")
void insertNewTable(@Param("p_test") String p_test);
如何解决此问题以获得返回值?
答案 0 :(得分:0)
您可以使用NamedStoredProcedureQuery
@NamedStoredProcedureQuery(
name = "insertNewTable",
procedureName = "insertNewTable",
parameters = {
@StoredProcedureParameter(mode = ParameterMode.IN, type = String.class, name
= "p_test"),
@StoredProcedureParameter(mode = ParameterMode.OUT, type = String.class, name
= "p_out"),
)
}
)
方法调用:
StoredProcedureQuery query =
this.em.createNamedStoredProcedureQuery("insertNewTable");
query.setParameter("p_test", ""TEST"");
query.execute();
String out = query.getOutputParameterValue("p_out");
答案 1 :(得分:0)
使用Spring DATA
在您的存储库中:
1)使用本机SQL调用:
@Query(值=“从对偶中选择存储过程名称(:param)”,nativeQuery = true) BigDecimal method-name(@Param(“ param”)Long param);
2)
@Procedure(procedureName = "store-procedure-name", outputParameterName = "output-param-of-store-procedure")
return-type-of-store-procedure method-name(input-params-of-store-procedure);