Skip to main content

Notice: This Wiki is now read only and edits are no longer 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/CRUDStoredProcedures"

(New page: CRUDStoredProcedures __TOC__ It is possible to override any EclipseLink generated SQL with custom SQL or a stored procedure call. For the CRUD (Create...)
 
Line 11: Line 11:
 
=== Oracle insert stored procedure ===
 
=== Oracle insert stored procedure ===
 
<source lang="sql">
 
<source lang="sql">
CREATE PROCEDURE EMP_INSERT (
+
CREATE PROCEDURE EMP_INSERT (
RESULT_CURSOR OUT CURSOR_TYPE.ANY_CURSOR) AS
+
P_EMP_ID NUMBER,
 +
P_SALARY NUMBER,
 +
P_F_NAME VARCHAR2,
 +
P_L_NAME VARCHAR2) AS
 
BEGIN  
 
BEGIN  
OPEN RESULT_CURSOR FOR Select e.*, s.* from EMPLOYEE e, SALARY s WHERE e.EMP_ID = s.EMP_ID;  
+
INSERT INTO EMPLOYEE (EMP_ID, F_NAME, L_NAME, SALARY) VALUES (P_EMP_ID, P_F_NAME, P_L_NAME, P_SALARY);  
 
END;
 
END;
 
</source>
 
</source>
Line 28: Line 31:
 
     StoredProcedureCall call = new StoredProcedureCall();
 
     StoredProcedureCall call = new StoredProcedureCall();
 
     call.setName("EMP_INSERT");
 
     call.setName("EMP_INSERT");
     call.addArgument();
+
     call.addArgument("P_EMP_ID", "EMP_ID");
 +
    call.addArgument("P_F_NAME", "F_NAME");
 +
    call.addArgument("P_L_NAME", "L_NAME");
 +
    call.addArgument("P_SALARY", "SALARY");
 
     descriptor.getQueryManager().setInsertCall(call);
 
     descriptor.getQueryManager().setInsertCall(call);
 
   }
 
   }
 
}
 
}
 
</source>
 
</source>

Revision as of 14:10, 9 May 2011

It is possible to override any EclipseLink generated SQL with custom SQL or a stored procedure call. For the CRUD (Create, Read, Update, Delete) operations these are by default generated by EclipseLink. Each of these operations can be overridden through using a DescriptorCustomizer and the DescriptorQueryManager API.

Custom SQL or stored procedures can be used to perform different functionality, or to adhere to specific security requirements or corporate policies. Note that usage of a stored procedure to execute the same SQL as would be generated will not improve performance.

Oracle insert stored procedure

CREATE PROCEDURE EMP_INSERT (	
	P_EMP_ID NUMBER,
	P_SALARY NUMBER,
	P_F_NAME VARCHAR2,
	P_L_NAME VARCHAR2) AS
BEGIN 
INSERT INTO EMPLOYEE (EMP_ID, F_NAME, L_NAME, SALARY) VALUES (P_EMP_ID, P_F_NAME, P_L_NAME, P_SALARY); 
END;

Using DescriptorCustomizer to use a stored procedure for insert

import org.eclipse.persistence.config.DescriptorCustomizer;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.queries.StoredProcedureCall;
 
public class MyCustomizer implements DescriptorCustomizer {
  public void customize(ClassDescriptor descriptor) {
    StoredProcedureCall call = new StoredProcedureCall();
    call.setName("EMP_INSERT");
    call.addArgument("P_EMP_ID", "EMP_ID");
    call.addArgument("P_F_NAME", "F_NAME");
    call.addArgument("P_L_NAME", "L_NAME");
    call.addArgument("P_SALARY", "SALARY");
    descriptor.getQueryManager().setInsertCall(call);
  }
}

Back to the top