I am confronting with an issue and it seems that many people encountered it and probably couldn\'t solve it.
I have the following MYSQL stored procedure. This is jus
I had the same error which got resolved by changing ParameterMode.OUT to ParameterMode.REF_CURSOR. I had written this mistakenly.
query.registerStoredProcedureParameter(12, ResultSet.class, ParameterMode.OUT);
query.registerStoredProcedureParameter(12, ResultSet.class, ParameterMode.REF_CURSOR);
You can try these two ways
Change below lines
@Procedure("ResourceType.getResourceTypes") List getResourceTypes();
like this
List<ResourceType> getResourceTypes(); // because as per JPA if your method name matches with the @NamedStoredProcedure -> name then no need to mention @Procedure annotation.
If you want to use @Procedure annotation then change the below lines
@Procedure("ResourceType.getResourceTypes") List getResourceTypes();
like this
@Procedure(name="getResourceTypes")
List<ResourceType> getResourceTypes();
Spring Data JPA as of 1.11.1 does not support SPs returning result sets. I filed a corresponding defect with Spring Data.
The solution is to descend an API level and just use JPA. Here is a generic class that I wrote that works with MS SQL SPs.
import com.google.common.base.Strings;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.ParameterMode;
import javax.persistence.Query;
import javax.persistence.StoredProcedureQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class StoredProcRepository {
//region Injected beans (via a RequiredArgsConstructor)
private final EntityManager em;
//endregion
/**
* Calls a stored procedure via JPA and retrieves a single implicit result set (in DBs that
* support them e.g. MS SQL or MySQL). The call is not dependent on a DB dialect. Be
* aware that large result sets should be paginated and not entirely read to memory. Recreates
* StoredProcedureQuery instance and its parameters on each call.
* To execute MS SQL SPs performing multiple queries, SET NOCOUNT ON.
*
* @param procedureName stored procedure name, optionally qualified per DB syntax
* @param resultClass converts (maps) each result set row into instances of resultClass via JPA
* @param spArgs stored procedure arguments, supplied positionally (optional SP arguments at the
* end of the list could be omitted)
* @param <T> class of row instances converted per JPA
* @return the entire result set
*/
public <T> List<T> queryViaStoredProc(String procedureName, Class<T> resultClass,
Object... spArgs) {
StoredProcedureQuery spq = em.createStoredProcedureQuery(procedureName, resultClass);
int pos = 0;
for (Object arg : spArgs) {
spq.registerStoredProcedureParameter(++pos, arg.getClass(), ParameterMode.IN);
spq.setParameter(pos, arg);
}
return spq.getResultList();
}
/**
* Calls a stored procedure via JPA and retrieves only the top row of a single implicit result
* set (in DBs that support them e.g. MS SQL or MySQL).
* Assumes that result set has at least one row.
* The call is not dependent on a DB dialect.
* Be aware that large result sets should be paginated and not entirely read to memory.
* Recreates StoredProcedureQuery instance and its parameters on each call.
* To execute MS SQL SPs performing multiple queries, SET NOCOUNT ON.
*
* @param procedureName stored procedure name, optionally qualified per DB syntax
* @param resultClass converts (maps) each result set row into instances of resultClass via JPA
* @param spArgs stored procedure arguments, supplied positionally (optional SP arguments at the
* end of the list could be omitted)
* @param <T> class of row instances converted per JPA
* @return the entire result set
*/
public <T> T queryTopRowViaStoredProc(String procedureName, Class<T> resultClass,
Object... spArgs) {
return queryViaStoredProc(procedureName, resultClass, spArgs).get(0);
}
}
For MS SQL SPs, the additional requirement is to have SET NOCOUNT ON
for all SPs executing more than one query. This could be set in one of at least three ways:
Her is code for #1: corresponding methods for the same StoredProcRepository
class.
/**
* Calls an MS SQL stored procedure via JPA and retrieves a single implicit result set.
* Protects against lack of SET NOCOUNT in stored procedures.
* This works with jTDS JDBC driver, but not with MS JDBC driver.
* Be aware that large result sets should be paginated and not entirely read to memory.
*
* @param procedureName stored procedure name, optionally qualified per DB syntax
* @param resultClass converts (maps) each result set row into instances of resultClass via JPA
* @param spArgs stored procedure arguments, supplied positionally (optional SP arguments at the
* end of the list could be omitted)
* @param <T> class of row instances converted per JPA
* @return the entire result set
*/
public <T> List<T> queryViaMsSqlStoredProc(String procedureName, Class<T> resultClass,
Object... spArgs) {
String spBindParams = (spArgs.length == 0) ? "" : "?" + Strings.repeat(",?", spArgs.length - 1);
// The following works with jTDS driver, but not with MS driver
String spQuery = String.format("EXEC %s %s", procedureName, spBindParams);
// The following works with jTDS driver, but not with MS driver
/*
String spQuery = String.format("{call %s(%s)}", procedureName, spBindParams);
Query q = em.createNativeQuery("SET NOCOUNT ON; " + spQuery, resultClass)
.setHint("org.hibernate.readOnly", true);
*/
Query q = em.createNativeQuery(spQuery, resultClass);
int pos = 0;
for (Object arg : spArgs) {
q.setParameter(++pos, arg);
}
return q.getResultList();
}
/**
* Calls an MS SQL stored procedure via JPA and retrieves only the top row of a single implicit
* result set.
* Assumes that result set has at least one row.
* The call sets the "NOCOUNT ON" MS SQL batch option.
* Be aware that large result sets should be paginated and not entirely read to memory.
*
* @param procedureName stored procedure name, optionally qualified per DB syntax
* @param resultClass converts (maps) each result set row into instances of resultClass via JPA
* @param spArgs stored procedure arguments, supplied positionally (optional SP arguments at the
* end of the list could be omitted)
* @param <T> class of row instances converted per JPA
* @return the entire result set
*/
public <T> T queryTopRowViaMsSqlStoredProc(String procedureName, Class<T> resultClass,
Object... spArgs) {
return queryViaMsSqlStoredProc(procedureName, resultClass, spArgs).get(0);
}
Problem seems to be while mapping your results (resulting entity can't be typed).
Try changing:
@Procedure("ResourceType.getResourceTypes")
List<ResourceType> getResourceTypes();
to
@Procedure("ResourceType.getResourceTypes")
Object[] getResourceTypes();
and remove the resultClasses in your NamedStoredProcedureQuery.
Hope that helps.