在 Oracle 数据库中我定义了类型:
create or replace TYPE person_type
AS OBJECT (id NUMBER(10), name VARCHAR2(50), age NUMBER);
和存储过程
create or replace PROCEDURE add_person (in_person IN person_type)
AS
BEGIN
INSERT into person (id, name, age) VALUES(in_person.id, in_person.name, person.age);
END;
我正在使用 Spring Boot 和 Hibernate,并且我想使用一些等效的 java bean 作为输入参数来调用该过程。 我见过很多例子,但它们只使用基本类型,而不是组合对象。 还有一些带有表注释的示例,但我不保证数据库中有这样的表,我只保证存储过程类型。
最佳答案
您需要StructDescriptor
获取 SQL 结构化对象,然后需要将其传递给 STRUCT
创建类型。
在StructDescriptor
中,您需要传递SQL对象名称,即PERSON_TYPE
和连接对象,即con
。
StructDescriptor StructDesc = StructDescriptor.createDescriptor("PERSON_TYPE", con);
然后,将结构描述符和对象数组传递给struct。请注意,数组索引代表其序列/位置,即在 SQL 对象上声明它(SQL 对象)的方式。因为 id
是第一个,然后是 name
,然后是 age
。
Object[] ObjArray = new Object[3];
ObjArray[0] = 1;
ObjArray[1] = "My Name";
ObjArray[2] = 23;
STRUCT structObj = new STRUCT(StructDesc, con, ObjArray);
现在,使用结构对象,您可以调用您的过程
stmt = (OracleCallableStatement) con.prepareCall("{call add_person(?)}");
stmt.setSTRUCT(1, structObj);
stmt.executeUpdate();
关于java - 如何以自定义对象作为输入参数调用Oracle存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48440817/