我有一个存储过程,它接收输入列表然后输出结果集。使用的数据库是SQL Server 2008,JPA 2.1规范提供程序是Hibernate。
我的实体结构如下:
@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
}
现在,我尝试使用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);
}
使用上面的代码,程序失败,“位置和命名参数混合无效”。
然后我试图忽略DATA JPA并尝试使用StoredProcedureQuery,如下所示
StoredProcedureQuery storedProcedureQuery = entityManagerFactory.createEntityManager().createStoredProcedureQuery("ModelSearchByModel",ModelSearchByModelEntity.class);
storedProcedureQuery.setParameter("RegionCode",regionCode);
//all other parameter setters..
return storedProcedureQuery.execute() ? storedProcedureQuery.getResultList() : null;
有了这个,我得到如下错误
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]
我不明白如何做到这一点。不确定是否有办法控制ParameterStrategy值。
这里有什么建议吗?
答案 0 :(得分:0)
您必须在设置之前注册参数:
awk
答案 1 :(得分:0)
如果要调用使用注释@NamedStoredProcedureQuery
声明的存储过程,则应使用方法createNamedStoredProcedureQuery
,而不是createStoredProcedureQuery
。后者适用于您不想使用注释的情况,在这种情况下您必须手动注册参数。
因此,如果您想使用注释,您可以编写类似
的内容entityManagerFactory.createEntityManager()
.createNamedStoredProcedureQuery("ModelSearchByModel", ModelSearchByModelEntity.class)
.setParameter("RegionCode",regionCode);
否则
StoredProcedureQuery storedProcedureQuery = entityManagerFactory.createEntityManager().createStoredProcedureQuery("SPR_ModelSearchByModel",ModelSearchByModelEntity.class);
storedProcedureQuery.registerStoredProcedureParameter("RegionCode", String.class , ParameterMode.IN);
// etc.
storedProcedureQuery.setParameter("RegionCode",regionCode);
// etc.
请注意,在第二个示例中,要使用的名称是存储过程的原始名称。