2017-05-01 114 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

非常感謝! –