Calling Oracle function with StoredProcedureQuery?

扶醉桌前 提交于 2019-12-24 21:26:52

问题


I'm trying to call an Oracle PL/SQL function using javax.persistence.StoredProcedureQuery and I get the error PLS-00221: 'function_name' is not a procedure or is undefined

So I assume its not possible to call an Oracle function using StoredProcedureQuery?

I don't see JPA offering an interface to call functions(as opposed to stored procedures). So how do I accomplish it?


回答1:


Sample Oracle Function:

CREATE OR REPLACE FUNCTION f_get_random_number
  RETURN NUMBER
AS
BEGIN
  RETURN TRUNC(dbms_random.value(1,100));
END;

Solution 1: using SQL Query

BigDecimal val = (BigDecimal)entityManager
                    .createNativeQuery("SELECT f_get_random_number FROM DUAL")
                    .getSingleResult();

Solution 2: using JDBC API

Session session = entityManager.unwrap( Session.class );         
    Integer out = session.doReturningWork( 
        connection -> {
        try (CallableStatement function = connection.prepareCall("{ ? = call f_get_random_number }" )) {
            function.registerOutParameter( 1, Types.INTEGER );
            function.execute();
            return function.getInt(1);
        } 
    } );


来源:https://stackoverflow.com/questions/25649326/calling-oracle-function-with-storedprocedurequery

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