开发者

Calling an oracle function from JPA

开发者 https://www.devze.com 2023-02-26 06:41 出处:网络
I am trying to call a simple function from a JPA clas开发者_开发百科s that returns a number based on some calculations and has the following definition.

I am trying to call a simple function from a JPA clas开发者_开发百科s that returns a number based on some calculations and has the following definition.

'CREATE OR REPLACE FUNCTION CFB.FC_AMOUNT_CHECK(accountNumber IN VARCHAR2)
return NUMBER IS .....'

I am trying to call this function from JPA the following way.

StringBuilder sql = new StringBuilder("call CFB.FC_AMOUNT_CHECK(:accountNumber)");
Query query = em.createNativeQuery(sql.toString());
query.setParameter(1, '1234');
List<?> result = query.getResultList();

....

However, when I execute this class, I get the below exception all the time:

java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: could not locate named parameter [1]

I cant seem to get how JPA cannot find parameter 1....I have been breaking my head with this for the last 4 hours. Can anyone please suggest how to get the result I want?


If you are looking to call a function in JPA select query then follow below link, it works:

http://www.eclipse.org/eclipselink/documentation/2.5/jpa/extensions/j_func.htm


If you want to set query parameters by index:

query.setParameter(1, '1234');

You need to change the named parameters inside the query, and replace it with a ?, like this:

StringBuilder sql = new StringBuilder("call CFB.FC_AMOUNT_CHECK(?)");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号