Hibernate JPA存储过程调用?

时间:2014-10-15 11:26:54

标签: java database hibernate jpa stored-procedures

我正在使用Hibernate JPA。我有以下Oracle存储过程。

CREATEORREPLACEPROCEDURE PROC_AB
(
      in_name VARCHAR2,
      in_lastname VARCHAR2,
      out_emp_id OUTINTEGER
)

如何调用此存储过程?

1 个答案:

答案 0 :(得分:1)

检查this SO question

  1. 首先定义名为native query的存储过程:

    @javax.persistence.NamedNativeQuery(name = "call_proc_ab", query = "{ call PROC_AB(:cmpid,:status,?) }", resultClass = Long.class, hints = {
    @javax.persistence.QueryHint(name = "org.hibernate.callable", value = "true") })
    
  2. 然后使用:

    执行它
    TypedQuery<Long> query = entityManager.createNamedQuery("call_proc_ab", Long.class); 
    query.setParameter("cmpid",cmpid); 
    query.setParameter("status",status); 
    Long empId = query.getSingleResult();