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/Examples/JPA/StoredProcedures"

(Using @NamedStoredProcedureQuery to define a stored procedure)
(Using @NamedStoredProcedureQuery to define a stored procedure)
Line 48: Line 48:
 
<source lang="java">
 
<source lang="java">
 
@NamedStoredProcedureQuery(name="findAllEmployees", procedureName="EMP_READ_ALL", resultClass=Employee.class, parameters={
 
@NamedStoredProcedureQuery(name="findAllEmployees", procedureName="EMP_READ_ALL", resultClass=Employee.class, parameters={
   @StoredProcedureParameter(queryParameter="RESULT_CURSORresult", name="result", direction=Direction.OUT_CURSOR)})
+
   @StoredProcedureParameter(queryParameter="RESULT_CURSOR", name="result", direction=Direction.OUT_CURSOR)})
 
@Entity
 
@Entity
 
public class Employee {
 
public class Employee {

Revision as of 08:03, 19 July 2012

EclipseLink has extended support for stored procedure execution including:

A stored procedure call be used in any query to read objects, or read or modify raw data.

Any CRUD or mapping operation can also be overridden using a stored procedure call using a DescriptorCustomizer and the DescriptorQueryManager API.

See,

Oracle stored procedure using OUT CURSOR

CREATE PROCEDURE EMP_READ_ALL (
	RESULT_CURSOR OUT CURSOR_TYPE.ANY_CURSOR) AS
BEGIN 
OPEN RESULT_CURSOR FOR SELECT e.*, s.* FROM EMPLOYEE e, SALARY s WHERE e.EMP_ID = s.EMP_ID; 
END;

Using JpaEntityManager createQuery() API to execute a stored procedure

import javax.persistence.Query;
import org.eclipse.persistence.queries.StoredProcedureCall;
import org.eclipse.persistence.queries.ReadAllQuery;
 
ReadAllQuery databaseQuery = new ReadAllQuery(Employee.class);
StoredProcedureCall call = new StoredProcedureCall();
call.setProcedureName("EMP_READ_ALL");
call.useNamedCursorOutputAsResultSet("RESULT_CURSOR");
databaseQuery.setCall(call);
 
Query query = ((JpaEntityManager)entityManager.getDelegate()).createQuery(databaseQuery);
List<Employee> result = query.getResultList();

Using @NamedStoredProcedureQuery to define a stored procedure

@NamedStoredProcedureQuery(name="findAllEmployees", procedureName="EMP_READ_ALL", resultClass=Employee.class, parameters={
  @StoredProcedureParameter(queryParameter="RESULT_CURSOR", name="result", direction=Direction.OUT_CURSOR)})
@Entity
public class Employee {
 ...
}

Using named query

Query query = entityManager.createNamedQuery("findAllEmployees");
List<Employee> result = query.getResultList();

Copyright © Eclipse Foundation, Inc. All Rights Reserved.