如何使用 Hibernate (EntityManager) 或 JPA 调用 Oracle 函数或过程
2022-09-04 06:31:08
我有一个Oracle函数,它返回sys-refcursor,当我使用Hibernate调用这个函数时,我得到以下异常。
Hibernate: { ? = call my_function(?) }
org.hibernate.exception.GenericJDBCException: could not execute query
javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1360)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1288)
at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:313)
如何解决此问题?
甲骨文函数
create or replace
FUNCTION my_function(p_val IN varchar2)
RETURN SYS_REFCURSOR
AS
my_cursor SYS_REFCURSOR;
BEGIN
OPEN my_cursor FOR SELECT emp_name FROM employees
WHERE lower(emp_name) like lower(p_val||'%');
RETURN my_cursor;
END;
我的实体类
@Entity
@javax.persistence.NamedNativeQuery(name = "getFunc", query =
"{ ? = call my_function(:empName) }",
resultClass = Employee.class, hints =
{ @javax.persistence.QueryHint(name = "org.hibernate.callable", value = "true") })
@Table(name = "EMPLOYEES")
和在 DAO 中
@Override
public void findEmployees(QueryData data,
String empName) {
List query = (List) entityManager.createNamedQuery("getFunc")
.setParameter("empName", empName)
.getSingleResult();
data.setResult(query);
}