【问题标题】:Quarkus JPA Postgres Stored Procedure call - use CALL ErrorQuarkus JPA Postgres 存储过程调用 - 使用 CALL 错误
【发布时间】:2021-12-02 14:48:15
【问题描述】:

我按照 Davide 在此处建议的方式从 Quarkus JPA 实现了一个 PostgresSQL 存储过程调用:

Quarkus REST API with Hibernate/Panache - Endpoints querying Postgres Stored Procedure

但是调用存储过程总是失败并出现以下错误:org.postgresql.util.PSQLException: ERROR: generate_sums(bigint, bigint, bigint) is an procedure 提示:要调用过程,请使用 CALL。

在做了一些研究后,我认为这是我的问题:

https://github.com/pgjdbc/pgjdbc/issues/1413

然而,根据这篇文章,这个问题已通过高于 42.2.16 的 Postgres JDBC 驱动程序版本得到修复:

Postgresql 11: Stored Procedure call error - To call a procedure, use CALL, Java

唯一的区别:他们在这里谈论的是 Spring,而不是 Quarkus。但是看看这个问题应该没什么区别。

我检查了我的 Quarkus 应用程序的 Maven 依赖树,发现它使用 42.2.22。所以一个包含这个修复的版本。

但是,我在尝试从 Quarkus 调用 PostgresSQL 存储过程时仍然收到此错误。

与 Davides 示例的唯一区别是我不提供 ResultClass,因为我的过程是一个 INSERT SELECT 语句,它根据其他表填充一个表并且不返回任何内容。

我正在使用 Quarkus 2.0。

【问题讨论】:

    标签: postgresql jpa quarkus


    【解决方案1】:

    前段时间我在使用 Quarkus 从 Oracle 执行存储过程时遇到了一些问题,我不确定,但我认为这是一个与您类似的错误。

    我在“registerStoredProcedureParameter”方法上设置了错误的类型。

    我将在这里放置执行两个不同存储过程的示例代码,希望对您有所帮助。

    第一个例子

    存储过程

    create or replace package proc_cliecoh_v2 is procedure execconh (v_param char);
    

    SQL 命令执行这个存储过程

    CALL PROC_CLIECOH_V2.EXECCONH( 'S' );
    

    Java 类

    import javax.enterprise.context.ApplicationScoped;
    import javax.inject.Inject;
    import javax.persistence.EntityManager;
    import javax.persistence.ParameterMode;
    import javax.persistence.PersistenceException;
    import javax.persistence.Query;
    import javax.persistence.StoredProcedureQuery;
    import javax.persistence.TypedQuery;
    import javax.transaction.Transactional;
    
    @ApplicationScoped
    public class MyExampleClass {
    
        @Inject
        EntityManager em;
    
        @Transactional
        public boolean firstExample() throws PersistenceException {
    
            StoredProcedureQuery storedProcedure = em.createStoredProcedureQuery("PROC_CLIECOH_V2.EXECCONH");
            storedProcedure.registerStoredProcedureParameter("v_param", String.class, ParameterMode.IN);
            storedProcedure.setParameter("v_param", "S");
            return storedProcedure.execute();
    
        }
    }
    

    第二个例子

    存储过程

    create or replace package proc_impclih_v2 is
     procedure execimph ( cd_empresa number, cd_usuario number, tp_ocorrencia char, cd_cliente_padrao number); 
    end proc_impclih;
    

    SQL 命令执行这个存储过程

    CALL PROC_IMPCLIH_V2.EXECIMPH( 123 , 1 , 'I' , 456 );
    

    Java 类

    import javax.enterprise.context.ApplicationScoped;
    import javax.inject.Inject;
    import javax.persistence.EntityManager;
    import javax.persistence.ParameterMode;
    import javax.persistence.PersistenceException;
    import javax.persistence.Query;
    import javax.persistence.StoredProcedureQuery;
    import javax.persistence.TypedQuery;
    import javax.transaction.Transactional;
    
    @ApplicationScoped
    public class MyExampleClass {
    
        @Inject
        EntityManager em;
    
    
        @Transactional
        public boolean secondExample() throws PersistenceException {
    
            StoredProcedureQuery storedProcedure = em.createStoredProcedureQuery("PROC_IMPCLIH_V2.EXECIMPH");
    
            storedProcedure.registerStoredProcedureParameter("cd_empresa", Integer.class, ParameterMode.IN);
            storedProcedure.registerStoredProcedureParameter("cd_usuario", Integer.class, ParameterMode.IN);
            storedProcedure.registerStoredProcedureParameter("tp_ocorrencia", String.class, ParameterMode.IN);
            storedProcedure.registerStoredProcedureParameter("cd_cliente_padrao", Integer.class, ParameterMode.IN);
    
            storedProcedure.setParameter("cd_empresa", 123);
            storedProcedure.setParameter("cd_usuario", 1);
            storedProcedure.setParameter("tp_ocorrencia", "I");
            storedProcedure.setParameter("cd_cliente_padrao", 456);
    
            return storedProcedure.execute();
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-18
      • 2018-10-04
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多