EclipseLink - Oracle Stored Procedure call newbie problem
I'm in charge of a task which I believe should be simple, but as I never did it before I have some trouble with it. I 开发者_运维百科have succesfully created an EJB 3 project using EclipseLink which will call a number of stored procedures from an Oracle database. I've configured the datasource correctly, I can connect and execute simple stored procedures and functions (without parameters and returning a cursor); however, I am currently unable to execute stored procedures with parameters.
I'm using the EclipseLink wiki as a reference http://wiki.eclipse.org/Using_Basic_Query_API_(ELUG) .
The code is:
StoredProcedureCall call = new StoredProcedureCall();
call.setProcedureName("p_environment.startSession");
List<String[]> args = new ArrayList<String[]>();
args.add(new String[] { "user", "ae01403" });
args.add(new String[] { "application", "app_code" });
args.add(new String[] { "locale", "it_IT" });
for (String[] pair : args) {
call.addNamedArgumentValue(pair[0], pair[1]);
}
DataReadQuery query = new DataReadQuery(call);
for (String[] pair : args) {
query.addArgument(pair[0]);
}
Based on database documentation, as I have no access to the database itself, the procedure takes 3 VARCHAR IN parameters of the specified name. I then call the executeQuery method on the active session, but receive the "Wrong type or number of arguments" error. What am I doing wrong? Any help is appreciated.
EDIT: The stored procedure signature, as per documentation, is:
p_environment.startSession(as_user IN VARCHAR2,
as_application IN VARCHAR2,
as_locale IN VARCHAR2);
Many thanks!
In your code, call.addNamedArgumentValue(pair[0], pair[1]);
sounds strange.
Don't you need to do this instead ?
call.addNamedArgument(procedureParameterName, argumentFieldName, argumentType);
It enables the mapping between your stored procedure parameters and the args you want to use.
For example, for you, it gives :
call.addNamedArgument(procedureUserParameterName, "user", String.class);
call.addNamedArgument(procedureUserParameterName, "application", String.class);
call.addNamedArgument(procedureUserParameterName, "locale", String.class);
You keep this :
DataReadQuery query = new DataReadQuery(call);
for (String[] pair : args) {
query.addArgument(pair[0]);
}
Then to call your Stored Procedure :
Session session = jpaEntityManager.getActiveSession();
List args = new ArrayList();
args.add(“ae01403”);
args.add(“app_code”);
args.add(“it_IT”);
List results = (List) session.executeQuery(query, args);
精彩评论