JPA + StoredProcedureCall + object type IN parameter

六月ゝ 毕业季﹏ 提交于 2019-12-12 20:40:31

问题


I'm trying to do a simple thing: call stored procedure which have a object type parameter.

This is what I have in db:

create or replace
TYPE TEST_TYPE AS OBJECT 
(
  test_field varchar(100)
)

and

CREATE OR REPLACE PROCEDURE TEST_PROC 
(
  PARAM1 IN TEST_TYPE 
) AS 
BEGIN
END TEST_PROC;

This is what I have in my java code:

@Embeddable
@Struct(name = "TEST_TYPE", fields = {"TEST_FIELD"})
public class TestStruct
{

    private String testField;

    public String getTestField() {
        return testField;
    }

    public void setTestField(String testField) {
        this.testField = testField;
    }
}

and

    @PostConstruct
    public void init()
    {

        StoredProcedureCall call = new StoredProcedureCall();
        call.setProcedureName("TEST_PROC");
        call.addNamedArgument("PARAM1", "PARAM1", Types.STRUCT, "TEST_TYPE", TestStruct.class);

        DataReadQuery dataReadQuery = new DataReadQuery(call);
        dataReadQuery.addArgument("PARAM1");

        TestStruct testStruct = new TestStruct();
        List args = new ArrayList();
        args.add(testStruct);

        Object result = ((EntityManagerImpl)em.getDelegate()).getSession().executeQuery(dataReadQuery,args);
    }

this is what I get in runtime:

Internal Exception: java.sql.SQLException: Invalid column type
Error Code: 17004
Call: BEGIN TEST_PROC(PARAM1=>?); END;
    bind => [1 parameter bound]
Query: DataReadQuery()

I think I totally don't understand the subject of usage structs with JPA

please help me, good people :)

What is the shortest way to make this working?


回答1:


Please send complete your code. For call stored procedures using Spring, you have to extends StoredProcedure class. If you send your complete code, I can help better. sample pseudo code:

class CustomStoredProcedure extends org.springframework.jdbc.object.StoredProcedure
{
    CustomStoredProcedure()
    {
        super([your-data-source], [package-name]);  
        declareParameter(new SqlParameter([your-struct-name]), Types.STRUCT));
        compile();
    }

    Map<String, Object> execute([your-parameter])
    {
        return super.execute(inputs);
    }
}

for better help, you have explain complete situation.




回答2:


Your code looks correct.

Ensure that the descriptor was defined for the struct. (i.e. session.getDescrptor(TestStruct.class))

Can you call stored procedures with other types?

What database are you using, have you set your platform correctly to Oracle?




回答3:


It seems that eclipselink skips descriptors for the @Struct and @Embeddable annotated classes unless they are referenced by some other class. The shortest way to make it working is to use workaround based on this assumption. Put additional class in the jar where your META-INF/persistence.xml is located:

@Entity
public class StructEntitiesWorkaround {

  @Id
  private String id;

  private TestStruct testStruct;

}



回答4:


You might want to use SimpleJdbcCall with Types.STRUCT.

Here is an example: https://docs.spring.io/spring-data/jdbc/old-docs/2.0.0.M1/reference/html/orcl.datatypes.html



来源:https://stackoverflow.com/questions/9129181/jpa-storedprocedurecall-object-type-in-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!