1

https://docs.spring.io/spring-data/jdbc/old-docs/current/reference/html/orcl.datatypes.html

DB:Postgres 尝试从 java 传递自定义类型作为上述 url 后面的过程的参数。

自定义类型:

CREATE TYPE schm.TESTTYPE AS
(
    tranProdCode character varying(20),
    particulars character varying(200),
    totalAmount numeric(18,4)
);

程序定义:

create or replace procedure schm.post (
   IN request schm.TESTTYPE
)
language plpgsql    
as $$

DECLARE

--xxxxxxxxxxxxxxxxxxxx

begin
-- xxxxxxxxxxxxxxxxxxxx        
end;$$
class SqlPostTransaction extends PostTransaction implements SQLData {

    @Override
    public String getSQLTypeName() throws SQLException {
        return "testtype";
    }

    @Override
    public void readSQL(SQLInput sqlInput, String s) throws SQLException {
        setTranProdCode(sqlInput.readString());
        setParticulars(sqlInput.readString());
        setTotalAmount(new BigDecimal(sqlInput.readString()));
    }

    @Override
    public void writeSQL(SQLOutput sqlOutput) throws SQLException {
        sqlOutput.writeString(getTranProdCode());
        sqlOutput.writeString(getParticulars());
        sqlOutput.writeBigDecimal(getTotalAmount());
    }
}
    @PersistenceContext
    private EntityManager entityManager;

    public DataSource getDataSourceFromHibernateEntityManager() {
        EntityManagerFactoryInfo info = (EntityManagerFactoryInfo)  entityManager.getEntityManagerFactory();
        return info.getDataSource();
    }

        SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(getDataSourceFromHibernateEntityManager())
                .withSchemaName("schm").withProcedureName("post")
                .declareParameters(
                        new SqlParameter("request", Types.STRUCT, "testtype"));

/*SqlPostTransaction sp - object*/
            Map in = Collections.singletonMap("request", sp);
            simpleJdbcCall.execute(in);

错误信息 :

如果使用 Types.OTHER

org.springframework.jdbc.BadSqlGrammarException: CallableStatementCallback; 糟糕的 SQL 语法 [{call schm.post(?)}]; 嵌套异常是 org.postgresql.util.PSQLException:错误:schm.post(unknown) 是一个过程 提示:要调用一个过程,请使用 CALL。职位:15

如果使用 Types.STRUCT

org.springframework.jdbc.BadSqlGrammarException: CallableStatementCallback; 糟糕的 SQL 语法 [{call schm.post(?)}]; 嵌套异常是 org.postgresql.util.PSQLException:不支持的类型值:2,002

也尝试过但没有运气 - escapeSyntaxCallMod=callIfNoReturn

https://github.com/pgjdbc/pgjdbc

其他有用的链接 -

https://www.highgo.ca/2020/11/25/calling-a-stored-procedure-in-postgrsql-from-java-and-its-current-limitations/

4

0 回答 0