Skip to content

Commit d802737

Browse files
author
Thomas Darimont
committed
DATAJPA-652 - Added support for REF_CURSOR output parameters for procedures.
We now support detecting output parameters for stored-procedures with ParameterMode.REF_CURSOR. Previously we only considered parameters with OUT or INOUT mode as output parameters. Note that since it is a bit tricky to setup a proper test case with hsql / eclipselink, I had to verify this with a standalone spring boot app: https://gist.github.com/thomasdarimont/129bc15d0ccc459610c2 A proper test case will follow. Original pull request: #130.
1 parent 049fc45 commit d802737

File tree

1 file changed

+39
-21
lines changed

1 file changed

+39
-21
lines changed

src/main/java/org/springframework/data/jpa/repository/query/StoredProcedureAttributeSource.java

+39-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -57,7 +57,7 @@ public StoredProcedureAttributes createFrom(Method method, JpaEntityMetadata<?>
5757
procedure);
5858

5959
if (namedStoredProc != null) {
60-
return newProcedureAttributesFrom(method, namedStoredProc);
60+
return newProcedureAttributesFrom(method, namedStoredProc, procedure);
6161
}
6262

6363
String procedureName = deriveProcedureNameFrom(method, procedure);
@@ -90,43 +90,61 @@ private String deriveProcedureNameFrom(Method method, Procedure procedure) {
9090
/**
9191
* @param method
9292
* @param namedStoredProc
93+
* @param procedure
9394
* @return
9495
*/
95-
private StoredProcedureAttributes newProcedureAttributesFrom(Method method, NamedStoredProcedureQuery namedStoredProc) {
96+
private StoredProcedureAttributes newProcedureAttributesFrom(Method method,
97+
NamedStoredProcedureQuery namedStoredProc, Procedure procedure) {
9698

9799
String outputParameterName = null;
98100
Class<?> outputParameterType = null;
99101

100-
int outputParameterCount = 0;
102+
if (!procedure.outputParameterName().isEmpty()) {
103+
104+
// we give the output parameter definition from the @Procedure annotation precedence
105+
outputParameterName = procedure.outputParameterName();
106+
} else {
107+
108+
// try to discover the output parameter
109+
List<StoredProcedureParameter> outputParameters = extractOutputParametersFrom(namedStoredProc);
110+
111+
if (outputParameters.size() != 1) {
112+
throw new IllegalStateException(String.format(
113+
"Could not create ProcedureAttributes from %s. We currently support exactly one output parameter!", method));
114+
}
115+
116+
StoredProcedureParameter outputParameter = outputParameters.get(0);
117+
outputParameterName = outputParameter.name();
118+
outputParameterType = outputParameter.type();
119+
}
120+
121+
if (outputParameterType == null || Object.class.equals(outputParameterType)
122+
|| void.class.equals(outputParameterType)) {
123+
outputParameterType = method.getReturnType();
124+
}
125+
126+
return new StoredProcedureAttributes(namedStoredProc.name(), outputParameterName, outputParameterType, true);
127+
}
128+
129+
private List<StoredProcedureParameter> extractOutputParametersFrom(NamedStoredProcedureQuery namedStoredProc) {
130+
131+
List<StoredProcedureParameter> outputParameters = new ArrayList<StoredProcedureParameter>();
101132

102133
for (StoredProcedureParameter param : namedStoredProc.parameters()) {
134+
103135
switch (param.mode()) {
104136
case OUT:
105137
case INOUT:
106-
107-
if (outputParameterCount > 0) {
108-
throw new IllegalStateException(
109-
String.format(
110-
"Could not create ProcedureAttributes from %s. We currently support only one output parameter!",
111-
method));
112-
}
113-
114-
outputParameterName = param.name();
115-
outputParameterType = param.type();
116-
117-
outputParameterCount++;
138+
case REF_CURSOR:
139+
outputParameters.add(param);
118140
break;
119141
case IN:
120142
default:
121143
continue;
122144
}
123145
}
124146

125-
if (outputParameterType == null) {
126-
outputParameterType = method.getReturnType();
127-
}
128-
129-
return new StoredProcedureAttributes(namedStoredProc.name(), outputParameterName, outputParameterType, true);
147+
return outputParameters;
130148
}
131149

132150
/**

0 commit comments

Comments
 (0)