gpt4 book ai didi

java - JPA 2.0 - 使用@NamedNativeQuery 的 Oracle 函数 - 错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:18:12 26 4
gpt4 key购买 nike

在 JPA 2.0 中,我试图调用一个 Oracle 函数,该函数返回一个 SYSREF CURSOR 并采用一个输入参数 (ID)。

要求:如果输入参数有一个ID值,则应该获取该条记录,如果输入值为null,则应该返回所有数据。

如果我传递输入参数 (ID),它工作正常,但如果我将 null 值传递给输入,我会收到此错误。

它在 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

关于java - JPA 2.0 - 使用@NamedNativeQuery 的 Oracle 函数 - 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32227975/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com