Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/JPA/PLSQLStoredFunction"

(New page: PLSQLStoredFunction __TOC__ EclipseLink has support for calling Oracle PLSQL stored functions that return complex PLSQL data-types such as RECORD type...)
 
(Oracle stored function returning PLSQL record type)
Line 12: Line 12:
 
=== Oracle stored function returning PLSQL record type ===
 
=== Oracle stored function returning PLSQL record type ===
 
<source lang="sql">
 
<source lang="sql">
CREATE PROCEDURE EMP_READ_ALL (
+
CREATE OR REPLACE PACKAGE EMP_PKG AS
RESULT_CURSOR OUT CURSOR_TYPE.ANY_CURSOR) AS
+
TYPE EMP_REC IS RECORD (F_NAME VARCHAR2(30), L_NAME VARCHAR2(30), SALARY NUMBER(10,2));
BEGIN
+
FUNCTION GET_EMP RETURN EMP_REC;
OPEN RESULT_CURSOR FOR Select e.*, s.* from EMPLOYEE e, SALARY s WHERE e.EMP_ID = s.EMP_ID;  
+
END EMP_PKG;
 +
</source>
 +
 
 +
<source lang="sql">
 +
CREATE OR REPLACE PACKAGE BODY EMP_PKG AS
 +
FUNCTION GET_EMP RETURN EMP_REC AS
 +
  P_EMP EMP_REC; "
 +
  BEGIN P_EMP.F_NAME := 'Bob'; P_EMP.F_NAME := 'Smith'; P_EMP.SALARY := 30000;
 +
  RETURN P_EMP;
 
END;
 
END;
 +
END EMP_PKG;
 +
</source>
 +
 +
An OBJECT type mirror for the EMP_REC type must also be defined.
 +
<source lang="sql">
 +
CREATE OR REPLACE TYPE EMP_REC AS OBJECT (F_NAME VARCHAR2(30), L_NAME VARCHAR2(30), SALARY NUMBER(10,2))
 
</source>
 
</source>
  

Revision as of 12:07, 5 May 2011

EclipseLink has support for calling Oracle PLSQL stored functions that return complex PLSQL data-types such as RECORD types and TABLE types. PLSQL types are not supported by Oracle JDBC, so these types must be translated to Oracle OBJECT types and VARRAY types. OBJECT types are returned as java.sql.Struct and VARRAY as java.sql.Array types in JDBC.

To call a stored function using PLSQL types the PLSQLStoredFunctionCall or @NamedPLSQLStoredFunctionQuery must be used. PLSQLStoredProcedureCall and @NamedPLSQLStoredProcedureQuery also exist for stored procedures. For regular stored functions and procedure that do not return complex PLSQL types the regular StoredFunctionCall, StoredProcedureCall and @NamedStoredFunctionQuery, @NamedStoredProcedureQuery can be used.

To call PLSQL stored functions or procedures mirror OBJECT/VARRAY types must be defined for the RECORD/TABLE types.

Oracle stored function returning PLSQL record type

CREATE OR REPLACE PACKAGE EMP_PKG AS
TYPE EMP_REC IS RECORD (F_NAME VARCHAR2(30), L_NAME VARCHAR2(30), SALARY NUMBER(10,2));
FUNCTION GET_EMP RETURN EMP_REC;
END EMP_PKG;
CREATE OR REPLACE PACKAGE BODY EMP_PKG AS
FUNCTION GET_EMP RETURN EMP_REC AS
  P_EMP EMP_REC; "
  BEGIN P_EMP.F_NAME := 'Bob'; P_EMP.F_NAME := 'Smith'; P_EMP.SALARY := 30000;
  RETURN P_EMP;
END;
END EMP_PKG;

An OBJECT type mirror for the EMP_REC type must also be defined.

CREATE OR REPLACE TYPE EMP_REC AS OBJECT (F_NAME VARCHAR2(30), L_NAME VARCHAR2(30), SALARY NUMBER(10,2))

Using JpaEntityManager createQuery() API to execute a PLSQL stored function

import javax.persistence.Query;
import org.eclipse.persistence.platform.database.orcle.plsql.PLSQLStoredFunctionCall;
import org.eclipse.persistence.queries.ReadAllQuery;
 
DataReadQuery databaseQuery = new DataReadQuery();
PLSQLStoredFunctionCall call = new PLSQLStoredFunctionCall ();
call.setProcedureName("GET_EMP");
call.
databaseQuery.setCall(call);
 
Query query = ((JpaEntityManager)entityManager.getDelegate()).createQuery(databaseQuery);
Employee result = (Employee)query.getSingleResult();

Using @NamedPLSQLStoredFunctionQuery to define a stored function

@NamedPLSQLStoredFunctionQuery(name="getEmployee", procedureName="GET_EMP",
    returnParameter=@PLSQLParameter(name="RESULT", databaseType="EMP_REC", javaType=Employee.class))
@Embeddable
@Struct(fields={"F_NAME", "L_NAME", "SALARY"})
@PLSQLRecord(name="EMP_REC")
public class Employee {
 @Column(name="F_NAME")
 private String firstName;
 @Column(name="L_NAME")
 private String lastName;
 @Column(name="SALARY")
 private BigDecimal salary;
 ...
}

Using named query

Query query = entityManager.createNamedQuery("getEmployee");
Employee result = (Employee)query.getSingleResult();