gpt4 book ai didi

具有多个命名参数和结果集的 Hibernate 存储过程调用不起作用

转载 作者:行者123 更新时间:2023-12-01 06:25:48 24 4
gpt4 key购买 nike

我有一个存储过程,它接受输入列表,然后输出结果集。使用的 DB 是 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 值。

这里有什么建议吗?

最佳答案

如果要调用使用注释 @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.

请注意,在第二个示例中,要使用的名称是存储过程的原始名称。

关于具有多个命名参数和结果集的 Hibernate 存储过程调用不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28841503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com