我有一个返回 sys-refcursor 的 Oracle 函数,当我使用 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);
}