Calling Oracle Stored Procedures from EclipseLink with multiple OUT parameters
In Oracle databases it is very customary to perform operations by calling PL/SQL stored procedures and functions. Currently I am developing a Java EE application with an Oracle database which basically means that lots of PL/SQL calls need to be done from Java. We have chosen EclipseLink as JPA layer. The EclipseLink WIKI contains many excellent examples on how to call PL/SQL stored procedures and functions. However, all examples I could find only show how to call these procedures and function when no or at most one return value is to be expected. What if the procedure contains more than one OUT parameter?
The answer lies in this JPA example on the EclipseLink WIKI. The example also only contains one OUT parameter. However, it also uses a “low level” DataReadQuery class, while all other examples use high level classes like ValueReadquery and DataModifyQuery. A ValueReadQuery only allows for one return value, a DataModifyQuery doesn’t allow for any return value at all. However, the javadoc of DataReadQuery states this class has the following Responsibilities: “Execute a selecting raw SQL string. This returns a Collection of the Records representing the result set.”. And this is exactly what I was after.
Consider the following stored procedure:
PROCEDURE two_args_out(x VARCHAR2, y OUT VARCHAR2, z OUT VARCHAR2) AS
BEGIN
y := 'Hello ' || x;
z := 'Bye ' || x;
END;
This procedure can be called from EclipseLink as follows, assuming you have injected an EntityManager that is called em:
JpaEntityManager jpaEntityManager = JpaHelper.getEntityManager(em);
Session session = jpaEntityManager.getActiveSession();
StoredProcedureCall spc = new StoredProcedureCall();
spc.setProcedureName("two_args_out");
spc.addNamedArgument("x");
spc.addNamedOutputArgument("y");
spc.addNamedOutputArgument("z");
DataReadQuery query = new DataReadQuery();
query.setCall(spc);
query.addArgument("x");
List args = new ArrayList();
args.add("Wouter");
List results = (List) session.executeQuery(query, args);
DatabaseRecord record = (DatabaseRecord)results.get(0);
String y = (String) record.get("y");
String z = (String) record.get("z");
After this, y should contain “Hello Wouter” and z “Bye Wouter”.

Comments are closed.