Skip to content

DATAJPA-652 - Added support for REF_CURSOR output parameters for procedures. #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.8.0.BUILD-SNAPSHOT</version>
<version>1.8.0.DATAJPA-652-SNAPSHOT</version>

<name>Spring Data JPA</name>
<description>Spring Data module for JPA repositories.</description>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -57,7 +57,7 @@ public StoredProcedureAttributes createFrom(Method method, JpaEntityMetadata<?>
procedure);

if (namedStoredProc != null) {
return newProcedureAttributesFrom(method, namedStoredProc);
return newProcedureAttributesFrom(method, namedStoredProc, procedure);
}

String procedureName = deriveProcedureNameFrom(method, procedure);
Expand Down Expand Up @@ -90,43 +90,63 @@ private String deriveProcedureNameFrom(Method method, Procedure procedure) {
/**
* @param method
* @param namedStoredProc
* @param procedure
* @return
*/
private StoredProcedureAttributes newProcedureAttributesFrom(Method method, NamedStoredProcedureQuery namedStoredProc) {
private StoredProcedureAttributes newProcedureAttributesFrom(Method method,
NamedStoredProcedureQuery namedStoredProc, Procedure procedure) {

String outputParameterName = null;
Class<?> outputParameterType = null;

int outputParameterCount = 0;
if (!procedure.outputParameterName().isEmpty()) {

// we give the output parameter definition from the @Procedure annotation precedence
outputParameterName = procedure.outputParameterName();
} else {

// try to discover the output parameter
List<StoredProcedureParameter> outputParameters = extractOutputParametersFrom(namedStoredProc);

if (outputParameters.size() != 1 && !void.class.equals(method.getReturnType())) {
throw new IllegalStateException(String.format(
"Could not create ProcedureAttributes from %s. We currently support exactly one output parameter!", method));
}

if (!outputParameters.isEmpty()) {
StoredProcedureParameter outputParameter = outputParameters.get(0);
outputParameterName = outputParameter.name();
outputParameterType = outputParameter.type();
}
}

if (outputParameterType == null || Object.class.equals(outputParameterType)
|| void.class.equals(outputParameterType)) {
outputParameterType = method.getReturnType();
}

return new StoredProcedureAttributes(namedStoredProc.name(), outputParameterName, outputParameterType, true);
}

private List<StoredProcedureParameter> extractOutputParametersFrom(NamedStoredProcedureQuery namedStoredProc) {

List<StoredProcedureParameter> outputParameters = new ArrayList<StoredProcedureParameter>();

for (StoredProcedureParameter param : namedStoredProc.parameters()) {

switch (param.mode()) {
case OUT:
case INOUT:

if (outputParameterCount > 0) {
throw new IllegalStateException(
String.format(
"Could not create ProcedureAttributes from %s. We currently support only one output parameter!",
method));
}

outputParameterName = param.name();
outputParameterType = param.type();

outputParameterCount++;
case REF_CURSOR:
outputParameters.add(param);
break;
case IN:
default:
continue;
}
}

if (outputParameterType == null) {
outputParameterType = method.getReturnType();
}

return new StoredProcedureAttributes(namedStoredProc.name(), outputParameterName, outputParameterType, true);
return outputParameters;
}

/**
Expand Down
116 changes: 116 additions & 0 deletions src/test/java/org/springframework/data/jpa/domain/sample/Dummy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.domain.sample;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedStoredProcedureQueries;
import javax.persistence.NamedStoredProcedureQuery;
import javax.persistence.ParameterMode;
import javax.persistence.StoredProcedureParameter;

import org.springframework.util.ObjectUtils;

/**
* Sample domain class representing used for Stored Procedure tests.
*
* @author Thomas Darimont
*/
@Entity
@NamedStoredProcedureQueries({ //
@NamedStoredProcedureQuery(name = "Dummy.procedureWith1InputAnd1OutputParameter",
procedureName = "procedure_in1_out1", parameters = {
@StoredProcedureParameter(mode = ParameterMode.IN, type = Integer.class),
@StoredProcedureParameter(mode = ParameterMode.OUT, type = Integer.class) }) //
,
@NamedStoredProcedureQuery(name = "Dummy.procedureWith1InputAndNoOutputParameter",
procedureName = "procedure_in1_out0", parameters = { @StoredProcedureParameter(mode = ParameterMode.IN,
type = Integer.class) }) //
,
@NamedStoredProcedureQuery(name = "Dummy.procedureWithNoInputAnd1OutputParameter",
procedureName = "procedure_in0_out1", parameters = { @StoredProcedureParameter(mode = ParameterMode.OUT,
type = Integer.class) }) //
,
@NamedStoredProcedureQuery(name = "Dummy.procedureWith1InputAnd1OutputParameterWithResultSet",
procedureName = "procedure_in1_out0_return_rs_no_update", parameters = {
@StoredProcedureParameter(mode = ParameterMode.IN, type = Integer.class),
@StoredProcedureParameter(mode = ParameterMode.REF_CURSOR, type = void.class) }) //
,
@NamedStoredProcedureQuery(name = "Dummy.procedureWith1InputAnd1OutputParameterWithResultSetWithUpdate",
procedureName = "procedure_in1_out0_return_rs_with_update", parameters = {
@StoredProcedureParameter(mode = ParameterMode.IN, type = Integer.class),
@StoredProcedureParameter(mode = ParameterMode.REF_CURSOR, type = void.class) }) //
,
@NamedStoredProcedureQuery(name = "Dummy.procedureWith1InputAndNoOutputParameterWithUpdate",
procedureName = "procedure_in1_out0_no_return_with_update", parameters = { @StoredProcedureParameter(
mode = ParameterMode.IN, type = String.class) }) //
})
public class Dummy {

@Id @GeneratedValue private Integer id;
private String name;

public Dummy() {}

public Dummy(String name) {
this.name = name;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "Dummy [id=" + id + ", name=" + name + "]";
}

@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(name);
}

@Override
public boolean equals(Object that) {

if (that == this) {
return true;
}

if (that == null) {
return false;
}

if (!(that instanceof Dummy)) {
return false;
}

return ObjectUtils.nullSafeEquals(this.name, ((Dummy) that).name);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2008-2014 the original author or authors.
* Copyright 2008-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -84,7 +84,7 @@ public class User {
@Lob private byte[] binaryData;

@ElementCollection private Set<String> attributes;

@Temporal(TemporalType.DATE) private Date dateOfBirth;

/**
Expand Down Expand Up @@ -367,7 +367,7 @@ public Set<String> getAttributes() {
public void setAttributes(Set<String> attributes) {
this.attributes = attributes;
}

public Date getDateOfBirth() {
return dateOfBirth;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2008-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.repository;

import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;

/**
* Testcase to run {@link StoredProcedureIntegrationTests} integration tests on top of EclipseLink.
*
* @author Thomas Darimont
*/
@ContextConfiguration(classes = { StoredProcedureIntegrationTests.Config.class })
@ImportResource("classpath:eclipselink.xml")
public class EclipseLinkStoredProcedureIntegrationTests extends StoredProcedureIntegrationTests {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.repository;

import org.junit.Ignore;
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;

/**
* Test case to run {@link StoredProcedureIntegrationTests} integration tests on top of OpenJpa. This is currently not
* supported since, the OpenJPA tests need to be executed with hsqldb1 which doesn't supported stored procedures.
*
* @author Thomas Darimont
*/
@Ignore
@ContextConfiguration(classes = { StoredProcedureIntegrationTests.Config.class })
@ImportResource("classpath:openjpa.xml")
public class OpenJpaStoredProcedureIntegrationTests extends StoredProcedureIntegrationTests {}
Loading