【发布时间】:2021-03-07 21:32:14
【问题描述】:
目前,我正在尝试使用在 Oracle 中创建的存储过程 (SP),来自 Spring Data,带有 JPA 和注释 @Procedure。
看起来很简单,但是该过程有一个 Table 类型参数(因为它是一个数组),并且一旦我在存储库中执行该方法,我就会收到以下错误消息:
Caused by: java.sql.SQLException: ORA-06550: line 1, column 7: PLS-00306: wrong number or types of arguments in call to 'SP_NAME' ORA-06550: line 1, column 7: PL/SQL: Statement ignored
SP_NAME(存储过程名称)具有以下声明:
PROCEDURE SP_NAME(mv_name IN MVIEWS_ARRAYS) as ...... more code
正如您在声明 SP 中看到的,它引用了 MVIEWS_ARRAYS 类型,即声明为 TYPE,如下所示:
create or replace TYPE MVIEWS_ARRAYS IS TABLE OF VARCHAR2 (1000);
当我尝试从 Oracle 客户端使用 SP 时,使用以下代码,它工作正常:
DECLARE
mv_name MVIEWS_ARRAYS := MVIEWS_ARRAYS ('1', 'Two', 'Something else');
BEGIN
SP_NAME(mv_name);
END;
但是,当我尝试从 JAVA 调用 SP 时,使用以下代码就像一个存储库的示例;
@Repository
public interface InterfaceRepositoryExample extends JpaRepository<SomeEntity, Integer>{
@Async
@Procedure("SP_NAME")
public void spName(String[] mvName);
}
我在参数类型中遇到错误(上面的错误)。
我已经搜索过这个,但我没有运气,Oracle中的表类型与Java中的一些原始或包装类型之间没有类型关系,此外,我发现很多没有响应的帖子以及一些代码从 Java 创建动态对象,但我不知道这是否是我正在搜索的。
有人知道我该如何解决这个错误吗?
问候。
已编辑:
我尝试了不同的方式,但我无法使用 SP。
1.- 使用 Array 代替 String[],(参考:https://docs.spring.io/spring-data/jdbc/old-docs/2.0.0.M1/reference/html/orcl.datatypes.html,6.2 Epigraph)
@Repository
public interface InterfaceRepositoryExample extends JpaRepository<SomeEntity, Integer>{
@Async
@Procedure("SP_NAME")
public void spName(Array mvName);
}
从我将 String[] 转换为 Array 的服务:
java.sql.Array sqlArray = dataSource.getConnection().unwrap(OracleConnection.class).createOracleArray("MVIEWS_ARRAYS ", data);
但我收到错误消息:
org.springframework.dao.InvalidDataAccessApiUsageException:类型 不能为空;嵌套异常是 java.lang.IllegalArgumentException:类型不能为空
2.- 使用 CallableStatement:
public void useSP(Array array) throws SQLException {
CallableStatement proc = null;
String sql = "{ call SP_NAME(?) }";
try{
proc = dataSource.getConnection().unwrap(OracleConnection.class).prepareCall(sql);
proc.setArray(1, array);
proc.execute();
}finally{
proc.close();
}
}
从我将 String[] 转换为 Array 的服务:
java.sql.Array sqlArray = dataSource.getConnection().unwrap(OracleConnection.class).createOracleArray("MVIEWS_ARRAYS ", data);
useSP(sqlArray);
但我收到错误消息:
java.sql.SQLException:ORA-06550:第 1 行,第 7 列:PLS-00306:错误 调用“SP_NAME”的参数数量或类型 ORA-06550:第 1 行, 第 7 列:PL/SQL:语句被忽略
【问题讨论】:
标签: java spring-data-jpa spring-data