如何使用EclipseLink作为输入或输出处理Oracle Store Procedure调用

时间:2010-03-11 15:58:54

标签: java oracle jpa plsql eclipselink

我做了一个概念验证,以确定使用EclipseLink调用存储过程的效率。我能够使用带有Scalar /原始数据类型的EclispeLink调用oracle存储过程(链接Integer,varchar等)。我想了解如何从EclipseLink处理Oracle Store过程,并将集合(Oracle类型/用户定义类型)作为输入或输出参数。如果有人帮助我通过一个例子来理解这一点,我将非常感激。

由于 MAK

这是我使用cursor作为输出实现的示例,我想用用户定义的oracle类型替换游标,这也在下面提到。使用Eclipselink从Java调用过程get_holiday_list_type。

任何帮助我都非常感激。

主要类

package services;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

import org.eclipse.persistence.logging.SessionLog;
import org.eclipse.persistence.platform.database.oracle.Oracle11Platform;
import org.eclipse.persistence.queries.DataReadQuery;
import org.eclipse.persistence.queries.StoredProcedureCall;
import org.eclipse.persistence.sessions.DatabaseLogin;
import org.eclipse.persistence.sessions.DatabaseRecord;
import org.eclipse.persistence.sessions.DatabaseSession;
import org.eclipse.persistence.sessions.Project;
import org.eclipse.persistence.sessions.Session;

public class TestEclipseLinkJPA {

    Session s;

    TestEclipseLinkJPA() {

        String DATABASE_USERNAME = "SD64_FIRMCONFIG";
        String DATABASE_PASSWORD = "sdbaseline";
        String DATABASE_URL = "jdbc:oracle:thin:@GWMPCMTD.ea:1527:GWMPCMTD";
        String DATABASE_DRIVER = "oracle.jdbc.driver.OracleDriver";

        DatabaseLogin login = new DatabaseLogin();
        login.setUserName(DATABASE_USERNAME);
        login.setPassword(DATABASE_PASSWORD);
        login.setConnectionString(DATABASE_URL);
        login.setDriverClassName(DATABASE_DRIVER);
        login.setDatasourcePlatform(new Oracle11Platform());
        ((DatabaseLogin) login).bindAllParameters();

        Project project = new Project(login);
        s = project.createDatabaseSession();
        s.setLogLevel(SessionLog.FINE);
        ((DatabaseSession) s).login();

    }

    public static void main(String args[]) {

        TestEclipseLinkJPA test = new TestEclipseLinkJPA();
        test.run();
    }

    protected void run() {
        testProcCursor();
    }

    /*
     * Run Proc with scalar input and cursor output
     */
    @SuppressWarnings("unchecked")
    private void testProcCursor() {
        List outList = new ArrayList();
        StoredProcedureCall call = new StoredProcedureCall();
        call.setProcedureName("test_holiday_pkg.get_holiday_list_cur");
        call.addNamedArgument("i_user_id");
        call.addNamedArgument("i_hol_year");
        call.useNamedCursorOutputAsResultSet("o_approvalreasonlist");

        DataReadQuery query = new DataReadQuery();
        query.setCall(call);
        query.addArgument("i_user_id");
        query.addArgument("i_hol_year");

        List<Integer> queryArgs = new ArrayList<Integer>();
        queryArgs.add(Integer.valueOf(50077));
        queryArgs.add(Integer.valueOf(2010));

        outList = (List) s.executeQuery(query, queryArgs);
        ListIterator<DatabaseRecord> litr = ((List<DatabaseRecord>) outList)
                .listIterator();
        while (litr.hasNext()) {
            DatabaseRecord record = (DatabaseRecord) litr.next();
            System.out.println("Value -->" + record.get("BSNS_OBJCT_ID"));
        }
    }

}

对象

package dataobjects;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class KeyValuePairTo implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -5022039632874557887L;
    @Id
    @Column(name = "BSNS_OBJCT_ID")
    private String key;
    @Column(name = "DSCRPTN")
    private String value;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

程序详情

drop type obj_key_value_array2;

CREATE OR REPLACE TYPE obj_key_value2 AS OBJECT (
   bsns_objct_id   VARCHAR2 (15 CHAR),
   dscrptn         VARCHAR2 (65 CHAR)
)
/

CREATE OR REPLACE TYPE obj_key_value_array2 AS TABLE OF obj_key_value2
/


CREATE OR REPLACE PACKAGE test_holiday_pkg1
AS
   PROCEDURE get_holiday_list_cur (
      i_user_id              IN       NUMBER,
      i_hol_year             IN       NUMBER,
      o_approvalreasonlist   OUT      sys_refcursor
   );

   PROCEDURE get_holiday_list_type (
      i_user_id              IN       NUMBER,
      i_hol_year             IN       NUMBER,
      o_approvalreasonlist   OUT      obj_key_value_array2
   );
END test_holiday_pkg1;
/

CREATE OR REPLACE PACKAGE BODY TEST_HOLIDAY_PKG1
AS
   PROCEDURE get_holiday_list_cur (
      i_user_id              IN       NUMBER,
      i_hol_year             IN       NUMBER,
      o_approvalreasonlist   OUT      sys_refcursor
   )
   AS
   BEGIN
      OPEN o_approvalreasonlist FOR
         SELECT to_char(BSNS_OBJCT_ID) bsns_objct_id, DSCRPTN
           FROM holiday
          WHERE hol_year = i_hol_year;
   END get_holiday_list_cur;

   PROCEDURE get_holiday_list_type (
      i_user_id              IN       NUMBER,
      i_hol_year             IN       NUMBER,
      o_approvalreasonlist   OUT      obj_key_value_array2
   )
   AS
   BEGIN
         SELECT obj_key_value2 (to_char(bsns_objct_id), dscrptn)
      BULK COLLECT INTO o_approvalreasonlist
        FROM holiday
       WHERE hol_year = i_hol_year;
   END get_holiday_list_type;
END test_holiday_pkg1;
/
show errors;

1 个答案:

答案 0 :(得分:0)

也许看看EclipseLink extensions for Stored Procedure Query。请注意,JPA扩展显然不可移植,如oracle特定类型(因此这会使JPA两次失败)。