How to call function using EclipseLink

Deadly 提交于 2019-12-06 10:49:09

问题


How to call an Oracle function which returns sys_refcursor using EclipseLink?

There is a documentation which states about calling a function, but not sure how to call a function which returns sys_refcursor.

http://eclipse.org/eclipselink/documentation/2.4/jpa/extensions/a_namedstoredfunctionquery.htm

I have tried as follows

@NamedStoredFunctionQuery(name = "findEmployees", 
                          functionName = "getEmps", 
                          parameters = 
                          { @StoredProcedureParameter(queryParameter = "user", 
                                                      name = "username", 
                                                      direction = Direction.IN, 
                                                      type = String.class)
            } , 
    returnParameter = @StoredProcedureParameter(queryParameter = "c_cursor")
)

Oracle Function

CREATE or REPLACE FUNCTION getEmps (username varchar2)
      RETURN SYS_REFCURSOR
   AS
   c_cursor   SYS_REFCURSOR;
   BEGIN
   OPEN c_cursor FOR 
   SELECT * FROM employees where emp_no=username;
   RETURN c_cursor;

However when I execute, I am getting the following errors

Internal Exception: java.sql.SQLException: ORA-06550: line 1, column 13: PLS-00382: expression is of wrong type ORA-06550: line 1, column 7: PL/SQL: Statement ignored

Error Code: 6550 Call: BEGIN ? := getEmps(username=>?); END; bind => [=> c_cursor, S7845] Query: DataReadQuery(name="findEmps" ) at org.eclipse.persistence.internal.jpa.QueryImpl.getDetailedException(QueryImpl.java:378) at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:260) at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:469)

How can I resolve this issue?


回答1:


I think you must specify the Direction of the functions parmeter

CREATE or REPLACE FUNCTION getEmps (username IN varchar2)
      RETURN SYS_REFCURSOR
   AS
   c_cursor   SYS_REFCURSOR;
   BEGIN
   OPEN c_cursor FOR 
   SELECT * FROM employees where emp_no=username;
   RETURN c_cursor;

Try it, please !




回答2:


Save the function on database and then execute it with FUNCTION call.

For example, i have an Oracle function called 'SUMACAMPO' that sums two columns of the DB:

And my query:

select Function('SUMACAMPO') from Table t

Java Code:

Query q = em.createQuery("select Function('SUMACAMPO') from Table t");
List<Object[]> resultado= q.getResultList();
LOG.info("Resultado consulta: {}",resultado);

so, the output of execute the query in LOG is:

Resultado consulta: [4413700]


来源:https://stackoverflow.com/questions/27961486/how-to-call-function-using-eclipselink

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!