【问题标题】:JPA calling stored procedure with argumentJPA用参数调用存储过程
【发布时间】:2017-05-01 15:02:53
【问题描述】:

我的 postgreSQL 数据库中有这个过程:

CREATE OR REPLACE FUNCTION getItemsForCategory(categoryId integer)
  RETURNS SETOF ITEM AS $_$
DECLARE
  result ITEM;
BEGIN
  FOR result IN SELECT *
                FROM item it
                  JOIN item_category itcat ON it.id = itcat.item_id WHERE itcat.category_id = categoryId LOOP
    RETURN NEXT result;
  END LOOP;

END; $_$ LANGUAGE 'plpgsql';

使用终端效果很好,但我无法使用 JPA 调用它。这是我的代码 sn-p(4 是参数 cateforyId 的值):

 transactions.begin();
 final StoredProcedureQuery storedProcedureQuery = entityManager.createStoredProcedureQuery("getItemsForCategory");
 storedProcedureQuery.setParameter(1,4).execute();
 final List<ItemEntity> itemEntityList = (List<ItemEntity>) storedProcedureQuery.getResultList();
 transactions.commit();

运行上面的代码后,我收到此错误:

Exception in thread "main" java.lang.IllegalArgumentException: 
You have attempted to set a parameter at position 1 which does not exist in this query string getItemsForCategory

有人知道如何正确设置参数的值吗?我还尝试使用 0 而不是 1 来设置参数,使用其他数据类型的参数(字符串、对象)调用 setParameter,但每次我收到类似的类似错误时,我都会收到类似的错误,如图所示。非常感谢

【问题讨论】:

    标签: java postgresql jpa plpgsql


    【解决方案1】:

    在设置值之前,您需要注册您的参数。

    spq.registerStoredProcedureParameter("categoryId", int.class, ParameterMode.IN);
    

    this link

    【讨论】:

    • 非常感谢朋友!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    相关资源
    最近更新 更多