2017-05-01 118 views
0

我在PostgreSQL数据库有这样的过程:JPA调用存储过程的参数

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调用它。这里是我的代码片段(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来设置参数,并使用其他数据类型的参数(String,Object)调用setParameter,但是每次我收到类似于此处显示的那种类似的错误。非常感谢

回答

0

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

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

参见this link

+0

非常感谢! –