JPA 2.0 - 使用@NamedNativeQuery 的 Oracle 函数 - 错误
JPA 2.0 - Oracle function using @NamedNativeQuery - Error
在 JPA 2.0 中,我试图调用一个 Oracle 函数,该函数 return 是一个 SYSREF CURSOR 并接受一个输入参数 (ID)。
要求:如果输入参数有 ID 值,则应获取该记录,如果输入值为 null,则应 return 所有数据。
如果我传递输入参数 (ID),它工作正常,但如果我将空值传递给输入,我会收到此错误。
它在 JDBC 上工作正常,我不确定我是否在某处缺少 JPA 语法...
错误:
Caused by: java.sql.SQLException: ORA-06550: line 1, column 13:
PLS-00306: wrong number or types of arguments in call to 'GET_ENTITIES'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
实体
@Entity
@Table(name = "ENTITIES")
@NamedNativeQuery(name = "getEntities", query = "{ ? = call pkg_entities.get_entities(:IN_ENTITY_ID)}", resultClass = Eentity.class, hints = { @QueryHint(name = "org.hibernate.callable", value = "true") })
存储库
@Primary
@Repository
@Transactional
public class EntityRepositoryJpa20Impl implements EntityRepository {
@PersistenceContext
EntityManager entityManager;
@Transactional(readOnly = true)
@Override
public List<Eentity> getEntities(String id) {
Query query = entityManager.createNamedQuery("getEntities");
query.setParameter("IN_ENTITY_ID", id);
return query.getResultList();
}
}
测试
@Test
public void testNotWorking() {
List<Eentity> entities = entityRepository.getEntities(null);
}
@Test
public void testWorksFine() {
List<Eentity> entities = entityRepository.getEntities("900001");
}
Oracle 函数
function get_entities(in_entity_id number) return sys_refcursor
is
v_cursor sys_refcursor;
begin
open v_cursor for
select
entity_id,
b,
c
from entities e
where e.entity_id = nvl(in_entity_id,e.entity_id)
and e.is_excluded = 'N'
order by e.entity_id;
return v_cursor;
end get_entities;
更新:-
如果我传递空字符串 ("") 它工作正常但当我传递 null 时不起作用。
我现在创建了两个 Java 方法,如下所示,
@Entity
@Table(name = "ENTITIES")
@NamedNativeQueries(value = {
@NamedNativeQuery(name = "getEntity", query = "{ ? = call pkg_entities.get_entities(:IN_ENTITY_ID)}", resultClass = Eentity.class, hints = { @QueryHint(name = "org.hibernate.callable", value = "true") }),
@NamedNativeQuery(name = "getEntities", query = "{ ? = call pkg_entities.get_entities()}", resultClass = Eentity.class, hints = { @QueryHint(name = "org.hibernate.callable", value = "true") }) })
public class Eentity implements Serializable {
您可以尝试通过添加默认参数值来修改您的 PL/SQL 包:
IN_ENTITY_ID IN NUMBER DEFAULT NULL
即在你的情况下:
function get_entities(in_entity_id number default null) return sys_refcursor
DEFAULT NULL
尝试将此添加到您的输入参数
在 JPA 2.0 中,我试图调用一个 Oracle 函数,该函数 return 是一个 SYSREF CURSOR 并接受一个输入参数 (ID)。
要求:如果输入参数有 ID 值,则应获取该记录,如果输入值为 null,则应 return 所有数据。
如果我传递输入参数 (ID),它工作正常,但如果我将空值传递给输入,我会收到此错误。
它在 JDBC 上工作正常,我不确定我是否在某处缺少 JPA 语法...
错误:
Caused by: java.sql.SQLException: ORA-06550: line 1, column 13:
PLS-00306: wrong number or types of arguments in call to 'GET_ENTITIES'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
实体
@Entity
@Table(name = "ENTITIES")
@NamedNativeQuery(name = "getEntities", query = "{ ? = call pkg_entities.get_entities(:IN_ENTITY_ID)}", resultClass = Eentity.class, hints = { @QueryHint(name = "org.hibernate.callable", value = "true") })
存储库
@Primary
@Repository
@Transactional
public class EntityRepositoryJpa20Impl implements EntityRepository {
@PersistenceContext
EntityManager entityManager;
@Transactional(readOnly = true)
@Override
public List<Eentity> getEntities(String id) {
Query query = entityManager.createNamedQuery("getEntities");
query.setParameter("IN_ENTITY_ID", id);
return query.getResultList();
}
}
测试
@Test
public void testNotWorking() {
List<Eentity> entities = entityRepository.getEntities(null);
}
@Test
public void testWorksFine() {
List<Eentity> entities = entityRepository.getEntities("900001");
}
Oracle 函数
function get_entities(in_entity_id number) return sys_refcursor
is
v_cursor sys_refcursor;
begin
open v_cursor for
select
entity_id,
b,
c
from entities e
where e.entity_id = nvl(in_entity_id,e.entity_id)
and e.is_excluded = 'N'
order by e.entity_id;
return v_cursor;
end get_entities;
更新:-
如果我传递空字符串 ("") 它工作正常但当我传递 null 时不起作用。 我现在创建了两个 Java 方法,如下所示,
@Entity
@Table(name = "ENTITIES")
@NamedNativeQueries(value = {
@NamedNativeQuery(name = "getEntity", query = "{ ? = call pkg_entities.get_entities(:IN_ENTITY_ID)}", resultClass = Eentity.class, hints = { @QueryHint(name = "org.hibernate.callable", value = "true") }),
@NamedNativeQuery(name = "getEntities", query = "{ ? = call pkg_entities.get_entities()}", resultClass = Eentity.class, hints = { @QueryHint(name = "org.hibernate.callable", value = "true") }) })
public class Eentity implements Serializable {
您可以尝试通过添加默认参数值来修改您的 PL/SQL 包:
IN_ENTITY_ID IN NUMBER DEFAULT NULL
即在你的情况下:
function get_entities(in_entity_id number default null) return sys_refcursor
DEFAULT NULL
尝试将此添加到您的输入参数