Eclipselink / JPA:如何使用'@NamedPLSQLStoredFunctionQuery'调用返回对象类型的pl / sql函数

时间:2017-03-30 12:32:21

标签: oracle jpa java-ee eclipselink

是否可以从eclipselink调用返回对象类型的pl / sql函数?

我们构建了一个测试用例,可以从支持表中正确选择对象类型。所以我们的实体似乎得到了恰当的定义。但是当调用pl / sql-function时,testcase会失败NoResultException

在eclipselink-log中,该语句看起来没问题,但返回参数的绑定为空。我们缺少什么?

[EL Finest]: connection: 2017-03-30 13:51:32.631--ServerSession(1014328909)--Connection(1807648168)--Thread(Thread[main,5,main])--Connection acquired from connection pool [default].
[EL Fine]: sql: 2017-03-30 13:51:32.632--ServerSession(1014328909)--Connection(1807648168)--Thread(Thread[main,5,main])--
DECLARE
  RESULTTARGET t_emp;
BEGIN
  RESULTTARGET := GET_EMP();
END;
  bind => []
[EL Finest]: connection: 2017-03-30 13:51:32.688--ServerSession(1014328909)--Connection(1807648168)--Thread(Thread[main,5,main])--Connection released to connection pool [default].

当我们调用pl / sql函数返回varchar2时它运行正常,绑定也没问题。也许我们在databaseType的参数定义中找不到正确的@NamedPLSQLStoredFunctionQuery

@Before
public void initEM() {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory( "test" );
    em = emf.createEntityManager();
}

@Test // SUCCEEDS
public void testFind() {
    EmpEntity e = em.find( EmpEntity.class, 2 );
    assertNotNull( e );
}

@Test // FAILES: javax.persistence.NoResultException: getSingleResult() did not retrieve any entities
public void testNamedFunctionCall() {
    EmpEntity e = em.createNamedQuery( "callEmpFunction", EmpEntity.class ).getSingleResult();
    assertNotNull( e );
}

@After
public void closeEM() {
    em.close();
}

对象类型,支持表和pl / sql-function定义为

create or replace type T_DEPT as object
( deptno   number
, deptname varchar2(40)
, loc      varchar2(40)
);

create or replace type T_ADDRESS as object
( street varchar2(40)
, city   varchar2(40)
);

create or replace type T_EMP as object
( address t_address
, empno   number
, ename   varchar2(40)
, dept    t_dept
);

create table EMP_OBJECTS
( obj_id  number not null primary key
, emp     t_emp  not null
);

create or replace function GET_EMP
return t_emp
is
    v_emp    t_emp;
begin
    select emp
    into   v_emp
    from emp_objects
    where obj_id = 2; 

    return v_emp;
end;
/

declare
    v_dept t_dept    := t_dept( 10, 'sales', 'beach' );
    v_addr t_address := t_address( 'here', 'there' );
    v_emp  t_emp     := t_emp( v_addr, 1, 'scott', v_dept );
begin
    insert into emp_objects
    ( obj_id, emp )
    values
    ( 2, v_emp );
    commit;
end;

我们的Java实体和可嵌入类是

@Entity
@Table(name="EMP_OBJECTS")
@NamedPLSQLStoredFunctionQuery( name = "callEmpFunction"
   , functionName = "GET_EMP"
   , returnParameter = @PLSQLParameter(name = "result", databaseType="t_emp" )
   )
public class EmpEntity {    
    @Id
    private int obj_id;   
    @Structure
    private Emp emp;

    ... getter and setter ...
}

@Embeddable
@Struct(name = "T_EMP", fields = { "addr", "empno", "name", "dept" } )
public class Emp {
    @Structure
    private Address addr;
    private int     empno;
    private String  name;
    @Structure
    private Dept    dept;
}

@Embeddable
@Struct(name="T_ADDRESS", fields={"street","city"})
public class Address {   
    private String street;    
    private String city;
}

@Embeddable
@Struct(name="T_DEPT", fields={"deptno","deptname","loc"})
public class Dept {    
    private int deptno;    
    private String deptname;    
    private String loc;
}

我们正在使用eclipselink 2.6.4和oracle 11.2数据库。

1 个答案:

答案 0 :(得分:2)

很抱歉回答我自己的问题。经过一番反复试验后,我们找到了两个我们想要分享的解决方案。

首先:以编程方式创建函数调用,并将结果类型设置为OracleObjectType。注意类型和过程名称的大写名称。这似乎很重要。

@Test
public void testFunctionCall() {

    OracleObjectType result = new OracleObjectType();
    result.setJavaType( Emp.class );
    result.setTypeName( "T_EMP" );

    DataReadQuery databaseQuery = new DataReadQuery();
    databaseQuery.setResultType(DataReadQuery.VALUE);

    PLSQLStoredFunctionCall call = new PLSQLStoredFunctionCall(result);
    call.setProcedureName("GET_EMP");
    databaseQuery.setCall(call);

    Query query = ((JpaEntityManager)em.getDelegate()).createQuery(databaseQuery);
    Emp e = (Emp)query.getSingleResult();

    assertNotNull( e );        
}

第二:每当您想在程序中使用@Struct @OracleObject时,似乎有必要对可嵌入类进行注释或函数调用。通过以下更改,我们以前失败的测试用例成功。

@Embeddable
@Struct( name = "T_EMP", fields = { "addr", "empno", "name", "dept" } )
@OracleObject( name = "T_EMP", javaType = Emp.class, fields = {} )
public class Emp {
    @Structure
    private Address addr;
    private int     empno;
    private String  name;
    @Structure
    private Dept    dept;
}

@Embeddable
@Struct( name="T_ADDRESS", fields={"street","city"} )
@OracleObject( name = "T_ADDRESS", javaType = Address.class, fields = {} )
public class Address {    
    private String street;    
    private String city;
}

@Embeddable
@Struct( name = "T_DEPT", fields = { "deptno", "deptname", "loc" } )
@OracleObject( name = "T_DEPT", javaType = Dept.class, fields = {} )
public class Dept {
    private int    deptno;
    private String deptname;
    private String loc;
}

也许对eclipselink有更深入了解的人可以对使用@Struct@OracleObject的冗余进行评论?