从JPA 2.1调用Oracle函数?

时间:2015-05-08 14:52:18

标签: java jpa jpa-2.1

我试图从JPA 2.1调用oracle函数并且收到此错误 -

org.hibernate.exception.GenericJDBCException: could not extract ResultSet 
...
Caused by: java.sql.SQLException: Missing IN or OUT parameter at index:: 6
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
...

定义了该功能 -

FUNCTION my_function (
      username        IN   VARCHAR2,
      case_number     IN   NUMBER,
      event_type_id   IN   NUMBER,
      arguments       IN   VARCHAR2,
      note            IN   VARCHAR2
   )
      RETURN NUMBER
   IS
      retval     NUMBER;
   BEGIN
      --  WORKING OF FUNCTION REMOVED
      retval := 1;

      RETURN retval;
END;

我通过 -

来呼唤这个
@NamedNativeQuery(
        name="my_function",query="{ call proc_history.create_parent_event(:username, :caseNumber, :eventTypeId, :arguments, :note) }"
)
public class MyJPAObject {
//
}

用我的DAO -

public Integer callMyFuntion() {

   Query query = em.createNamedQuery("my_function");

   query.setParameter("username", "me");
   query.setParameter("caseNumber", 123456);
   query.setParameter("eventTypeId", 123);
   query.setParameter("arguments", "test");
   query.setParameter("note", "test");

   return (Integer)query.getSingleResult();
}

我已经确认我可以使用 -

调用该函数
DECLARE retval NUMBER; 
BEGIN retval := my_function('me', 123456, 123, 'test', 'test'); 
END;

我需要做什么才能通过JPA调用它?

1 个答案:

答案 0 :(得分:0)

如果函数返回数据,它应该是这样的:

query = "{ ? = call proc_history.create_parent_event(:username, :caseNumber, :eventTypeId, :arguments, :note) }"