Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/sandbox/gelernter/Extended Stored Procedure and PLSQL"

m
m
Line 19: Line 19:
 
|examples=y
 
|examples=y
 
|example=
 
|example=
*[[EclipseLink/Examples/JPA/PLSQLStoredFunction PLSQLStoredFuntion]]
+
*[[EclipseLink/Examples/JPA/PLSQLStoredFunction |PLSQLStoredFuntion]]
 
}}
 
}}
  
Line 26: Line 26:
 
=PLSQL Stored Function Support=
 
=PLSQL Stored Function Support=
  
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 <tt>java.sql.Struct</tt> and VARRAY as <tt>java.sql.Array</tt> types in JDBC.
+
EclipseLink provides 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 <tt>java.sql.Struct</tt> and VARRAY as <tt>java.sql.Array</tt> types in JDBC.
  
To call a stored function using PLSQL types the [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/platform/database/oracle/plsql/PLSQLStoredFunctionCall.html PLSQLStoredFunctionCall] or [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/NamedPLSQLStoredFunctionQuery.html @NamedPLSQLStoredFunctionQuery] must be used.  [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/platform/database/oracle/plsql/PLSQLStoredProcedureCall.html PLSQLStoredProcedureCall] and [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/NamedPLSQLStoredProcedureQuery.html @NamedPLSQLStoredProcedureQuery] also exist for stored procedures.  For regular stored functions and procedure that do not return complex PLSQL types the regular [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/queries/StoredFunctionCall.html StoredFunctionCall], [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/queries/StoredProcedureCall.html StoredProcedureCall] and [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/NamedStoredFunctionQuery.html @NamedStoredFunctionQuery], [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/NamedStoredProcedureQuery.html @NamedStoredProcedureQuery] can be used.
+
Use either of the following to call a stored function using PLSQL types:
 +
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/platform/database/oracle/plsql/PLSQLStoredFunctionCall.html PLSQLStoredFunctionCall]  
 +
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/NamedPLSQLStoredFunctionQuery.html @NamedPLSQLStoredFunctionQuery]  
 +
 
 +
Use either of the following to call a stored procedure:
 +
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/platform/database/oracle/plsql/PLSQLStoredProcedureCall.html PLSQLStoredProcedureCall]  
 +
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/NamedPLSQLStoredProcedureQuery.html @NamedPLSQLStoredProcedureQuery]  
 +
 
 +
Use the following for regular stored functions and procedure that do not return complex PLSQL types:
 +
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/queries/StoredFunctionCall.html StoredFunctionCall]
 +
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/queries/StoredProcedureCall.html StoredProcedureCall]  
 +
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/NamedStoredFunctionQuery.html @NamedStoredFunctionQuery]
 +
[http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/NamedStoredProcedureQuery.html @NamedStoredProcedureQuery]  
 +
 
 +
To call PLSQL stored functions or procedures, you must define mirror OBJECT/VARRAY types for the RECORD/TABLE types. Use the [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/Struct.html @Struct] annotation  to map OBJECT types to <tt>@Entity</tt> or <tt>@Embeddable</tt> classes.  Normally, you can use <tt>@Embeddable</tt>, unless the OBJECT type defines an Id and can be stored in a table.
 +
 
 +
Use the following annotations to map nested OBJECT and VARRAY types:
 +
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/Structure.html @Structure]
 +
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/Array.html @Array]
 +
 
 +
 
 +
==Examples==
 +
 
 +
===Oracle Stored Function Returning PLSQL Record Type===
  
To call PLSQL stored functions or procedures mirror OBJECT/VARRAY types must be defined for the RECORD/TABLE types.
 
OBJECT types can be mapped to @Entity or @Embeddable classes using the [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/Struct.html @Struct] annotation.  Normally @Embeddable is used, unless the OBJECT type defines an Id and can be stored in a table.
 
Nested OBJECT and VARRAY types can be mapped using the [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/Structure.html @Structure] and [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/Array.html @Array] annotations.
 
  
=== Oracle stored function returning PLSQL record type ===
 
 
<source lang="sql">
 
<source lang="sql">
 
CREATE OR REPLACE PACKAGE EMP_PKG AS
 
CREATE OR REPLACE PACKAGE EMP_PKG AS
Line 53: Line 72:
  
 
An OBJECT type mirror for the EMP_REC type must also be defined.
 
An OBJECT type mirror for the EMP_REC type must also be defined.
 +
 
<source lang="sql">
 
<source lang="sql">
 
CREATE OR REPLACE TYPE EMP_TYPE AS OBJECT (F_NAME VARCHAR2(30), L_NAME VARCHAR2(30), SALARY NUMBER(10,2))
 
CREATE OR REPLACE TYPE EMP_TYPE AS OBJECT (F_NAME VARCHAR2(30), L_NAME VARCHAR2(30), SALARY NUMBER(10,2))
 
</source>
 
</source>
  
=== Defining Java class to map to OBJECT type ===
+
=== Defining a Java Class to Map to OBJECT type ===
 +
 
 
<source lang="java">
 
<source lang="java">
 
@Embeddable
 
@Embeddable
Line 72: Line 93:
 
</source>
 
</source>
  
=== Using JpaEntityManager createQuery() API to execute a PLSQL stored function ===
+
=== Using JpaEntityManager createQuery() API to Execute a PLSQL Stored Function ===
 +
 
 
<source lang="java">
 
<source lang="java">
 
import javax.persistence.Query;
 
import javax.persistence.Query;
Line 95: Line 117:
 
</source>
 
</source>
  
=== Using @NamedPLSQLStoredFunctionQuery to define a stored function ===
+
=== Using @NamedPLSQLStoredFunctionQuery to Define a Stored Function ===
 +
 
 
<source lang="java">
 
<source lang="java">
 
@NamedPLSQLStoredFunctionQuery(name="getEmployee", functionName="EMP_PKG.GET_EMP",
 
@NamedPLSQLStoredFunctionQuery(name="getEmployee", functionName="EMP_PKG.GET_EMP",
Line 113: Line 136:
 
Employee result = (Employee)query.getSingleResult();
 
Employee result = (Employee)query.getSingleResult();
 
</source>
 
</source>
 +
  
 
===****************END OF EXAMPLE SOURCE888888888888888888===
 
===****************END OF EXAMPLE SOURCE888888888888888888===

Revision as of 11:31, 30 June 2011

EclipseLink JPA


******SANDBOX VERSION******

PLSQL Stored Function Support

EclipseLink provides 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.

Use either of the following to call a stored function using PLSQL types:

Use either of the following to call a stored procedure:

Use the following for regular stored functions and procedure that do not return complex PLSQL types:

To call PLSQL stored functions or procedures, you must define mirror OBJECT/VARRAY types for the RECORD/TABLE types. Use the @Struct annotation to map OBJECT types to @Entity or @Embeddable classes. Normally, you can use @Embeddable, unless the OBJECT type defines an Id and can be stored in a table.

Use the following annotations to map nested OBJECT and VARRAY types:


Examples

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_TYPE AS OBJECT (F_NAME VARCHAR2(30), L_NAME VARCHAR2(30), SALARY NUMBER(10,2))

Defining a Java Class to Map to OBJECT type

@Embeddable
@Struct(name="EMP_TYPE", fields={"F_NAME", "L_NAME", "SALARY"})
public class Employee {
 @Column(name="F_NAME")
 private String firstName;
 @Column(name="L_NAME")
 private String lastName;
 @Column(name="SALARY")
 private BigDecimal salary;
 ...
}

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();
PLSQLrecord record = new PLSQLrecord();
record.setTypeName("EMP_PKG.EMP_REC");
record.setCompatibleType("EMP_TYPE");
record.setJavaType(Employee.class);
record.addField("F_NAME", JDBCTypes.VARCHAR_TYPE, 30);
record.addField("L_NAME", JDBCTypes.VARCHAR_TYPE, 30);
record.addField("SALARY", JDBCTypes.NUMERIC_TYPE, 10, 2);
PLSQLStoredFunctionCall call = new PLSQLStoredFunctionCall(record);
call.setProcedureName("EMP_PKG.GET_EMP");
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", functionName="EMP_PKG.GET_EMP",
    returnParameter=@PLSQLParameter(name="RESULT", databaseType="EMP_PKG.EMP_REC"))
@Embeddable
@Struct(name="EMP_TYPE", fields={"F_NAME", "L_NAME", "SALARY"})
@PLSQLRecord(name="EMP_PKG.EMP_REC", compatibleType="EMP_TYPE", javaType=Employee.class,
    fields={@PLSQLParameter(name="F_NAME"), @PLSQLParameter(name="L_NAME"), @PLSQLParameter(name="SALARY", databaseType="NUMERIC_TYPE")})
public class Employee {
 ...
}

Using named query

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


****************END OF EXAMPLE SOURCE888888888888888888

Extended Stored Procedure and Function Support

This document describes the EclipseLink 2.3 extended stored procedure and function support.

The extended stored procedure support includes:

  • PLSQL stored functions
  • Multiple result sets
  • Multiple cursor output parameters
  • Output cursors of record types
  • Optional arguments
  • JPA annotation and XML support for stored functions
  • JPA annotation and XML support for PLSQL procedures and functions


These enhancements can be used by JPA, DBWS, and BPEL. ////Any other? Why is BPEL in here?////

Functionality

The PLSQLStoredFunctionCall class (a subclass of PLSQLStoredProcedureCall) provides the existing PLSQL data-type support for stored functions. ////existing in previous EL or in PLSQL?////

StoredProcedureCall now supports multiple result sets and multiple out cursors.

A flag will be added for multiple result sets, ////what is that flag? I don't see it in the Javadoc -- ah, but look below under API:////

  • StoredProcedureCall
    • setHasMultipleResultSets(boolean)
    • addNamedCursorOutputArgument(String)
    • addUnamedCursorOutputArgument(String)
    • addOptionalArgument(String)

BUT THESE ARE NOT IN THE JAVADOC. ARE THEY IMPLEMENTED? ////

or data queries will be supported, ////Does that mean eitherthe flag orthe queries will be supported? Which? Both? Again, I don't see anything in the Javadoc that applies to "data queries," whatever that means in this context////

unless both result sets are for the same class.

Specification of multiple out cursors is already supported from JPA, the support will allow their execution. Only data queries are supported, each out cursor will contains a List of DatabaseRecords stored in the output row the same as other output parameters.

Queries will support an optional argument for stored procedures that have parameters that define a default value. If the query argument is null, and the StoredProcedureCall defines the argument to be optional, then the query's SQL will be regenerate to omit the parameter.

JPA annotation and XML support will be added for stored functions the same as stored procedures, as well as the new options to the existing stored procedure annotation and XML.

JPA annotation and XML support will be added for PLSQL procedures and functions, this will require adding annotations for Record, Table, Struct, and Array types.

Testing

Extend existing foundation and JPA stored procedure tests for new types of procedure being supported.

API

  • StoredProcedureCall
    • setHasMultipleResultSets(boolean)
    • addNamedCursorOutputArgument(String)
    • addUnamedCursorOutputArgument(String)
    • addOptionalArgument(String)
  • DatabaseQuery
    • addArgument(String argumentName, Class type, boolean nullable)
  • @NamedStoredProcedureQuery
    • callByIndex : boolean
    • multipleResultSets : boolean
  • @StoredProcedureParameter
    • optional : boolean
  • PLSQLStoredFunctionCall
    • setResult(DatabaseType)
  • @NamedStoredFunctionQuery
    • returnParameter : @StoredProcedureParameter
  • @NamedPLSQLStoredProcedureQuery (org.eclipse.persistence.platform.oracle.annotations)
  • @NamedPLSQLStoredFunctionQuery (org.eclipse.persistence.platform.oracle.annotations)
  • @PLSQLParameter (org.eclipse.persistence.platform.oracle.annotations)
  • @PLSQLRecord (org.eclipse.persistence.platform.oracle.annotations)
  • @PLSQLTable (org.eclipse.persistence.platform.oracle.annotations)
  • @Struct
  • @Array
  • @Structure

Config files

  • orm.xml
<name-stored-function-query function-name="CHECK_EMP">
    <parameter name="EMP_ID"/>
    <return-parameter name="STATUS" type="java.lang.Boolean"/>
</name-stored-function-query>
<plsql-name-stored-function-query function-name="GET_EMP">
    <parameter name="EMP_ID" database-type="NUMERIC"/>
    <return-parameter name="EMP" database-type="EMP"/>
</plsql-name-stored-function-query>
<plsql-record type-name="EMP" compatible-type="EMP_TYPE" java-type="org.acme.Employee">
    <field name="EMP_ID" database-type="NUMERIC"/>
    <field name="NAME" database-type="VARCHAR"/>
</plsql-record>

Documentation

Should be documented under query stored procedure section.

Open Issues

Issue # Owner Description / Notes
1 What package to add JPA annotations to?
2

Decisions

Issue Description / Notes Decision

Future Considerations

  • Other procedure functionality.

Headline text

Eclipselink-logo.gif
Version: DRAFT
Other versions...

Back to the top