I have a stored procedure that takes a list of inputs and then outputs the result set. The database is used by SQL Server 2008, and the provider of the JPA 2.1 specification is Hibernate.
I have an entity structure as shown below:
@NamedStoredProcedureQuery(name = "ModelSearchByModel",
resultClasses = ModelSearchByModelEntity.class,
procedureName = "SPR_ModelSearchByModel",
parameters = {
@StoredProcedureParameter(mode = ParameterMode.IN, name = "RegionCode", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "BuildingCode", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "ProductCode", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "StatusCode", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "MinWidth", type = Integer.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "MaxWidth", type = Integer.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "MinDepth", type = Integer.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "MaxDepth", type = Integer.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "MinArea", type = Integer.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "MaxArea", type = Integer.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "MinImperviousArea", type = Integer.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "MaxImperviousArea", type = Integer.class)
})
@Entity
public class ModelSearchByModelEntity implements Serializable{
//Properties with @Id, @Basic and @Column annotations, along with getters and setters
}
Now I tried to use Spring DATA JPA
public interface IModelSearchByModelRepository extends Repository<ModelSearchByModelEntity, String> {
@Procedure(name = "ModelSearchByModel")
List<ModelSearchByModelEntity> findByCriteria(@Param("RegionCode") String regionCode,
@Param("BuildingCode") String buildingCode,
@Param("ProductCode") String productCode,
@Param("StatusCode") String statusCode,
@Param("MinWidth") Integer minWidth,
@Param("MaxWidth") Integer maxWidth,
@Param("MinDepth") Integer minDepth,
@Param("MaxDepth") Integer maxDepth,
@Param("MinArea") Integer minArea,
@Param("MaxArea") Integer maxArea,
@Param("MinImperviousArea") Integer minImperviousArea,
@Param("MaxImperviousArea") Integer maxImperviousArea);
}
With the above code, the program crashes with "Invalid mix of positional and named parameters."
Then I tried to ignore DATA JPA and tried to use StoredProcedureQuery as shown below
StoredProcedureQuery storedProcedureQuery = entityManagerFactory.createEntityManager().createStoredProcedureQuery("ModelSearchByModel",ModelSearchByModelEntity.class);
storedProcedureQuery.setParameter("RegionCode",regionCode);
return storedProcedureQuery.execute() ? storedProcedureQuery.getResultList() : null;
In doing so, I get an error as shown below
Caused by: java.lang.IllegalArgumentException: Parameter with that name [ArchRegionCode] did not exist
at org.hibernate.jpa.spi.BaseQueryImpl.findParameterRegistration(BaseQueryImpl.java:503) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
I don’t understand how to do it right. Not sure if there is a way to control the value of ParameterStrategy.
Any suggestions here?