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

标签 java oracle hibernate jpa-2.0

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

相关文章:

java - logcat 在 Android 中显示无法解释的 libgps 错误

sql - Oracle 11g SQL 在多列查询的一列中获取唯一值

java - 错误 : detached entity passed to persist - try to persist complex data (Play-Framework)

java - 文件更改后 MD5 哈希值未更改

javac - 生成特定于平台的二进制代码

sql select with 不包含

java - onSave()(对于使用 Hibernate/Spring Data Repositories 保存的任何实体)

Java Hibernate 多对一问题

java - Eclipse - 是否可以跳转到特定包?

oracle - 在 ODP.NET(用户定义类型)中使用 Oracle UDT 时出现奇怪问题