Calling an oracle function from JPA
41,756
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
Author by
Prashanth
Updated on November 26, 2020Comments
-
Prashanth over 3 years
I am trying to call a simple function from a JPA class 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?
-
Guru about 13 yearsNever worked in Hibernate. Not sure: is this line right?
query.setParameter(1, '1234');
-
Prashanth about 13 yearsYes, that is a valid statement in JPA.
-
Guru about 13 yearscan you try
query.setParameter("accountNumber", '1234');
and let me know the result for me? -
Guru about 13 yearsAlso not sure why you want to use bind variable when you are replacing it with literals. If possible change this
"call CFB.FC_AMOUNT_CHECK(accountNumber)"
and then tryquery.setParameter("accountNumber", '1234');
-
Bozho about 13 yearspossible duplicate of Calling stored procedure from Java / JPA
-
Nayan Wadekar about 13 yearshave you tried with setting parameter value directly in sql as - "call CFB.FC_AMOUNT_CHECK("+accountNumber+")"
-
Prashanth about 13 yearsYes Nayan - I tried to put in the value as you have suggested but with no luck. I think this is a fundamental problem with JPA and function calls. JPA seems to be lagging in terms of implementing a standard way of invoking SPs and functions in general.
-
Nayan Wadekar about 13 yearsI was able to call it, but without any in/out parameters, but no clue about to passing parameters.
-
JanM almost 10 yearsAs far as I know JPA doesn't support named parameters binding in native queries... you should stick to ? and indexed binding.
-
Vadzim over 7 yearsPossible duplicate of How to call a custom Oracle function returning a value from JPA
-