【问题标题】:StoredProcedure invocation using Spring JPA results in error使用 Spring JPA 调用 StoredProcedure 会导致错误
【发布时间】:2015-03-25 12:45:07
【问题描述】:

我正在使用休眠 JPA 在 Spring 应用程序中执行存储过程,但它导致以下错误。 StoredPrcoedure 没有任何参数,因此下面的错误并不直观。围绕此的任何谷歌搜索都没有结果,并且对 spring/hibernate 文档的引用也没有帮助。对此的任何帮助将不胜感激。我似乎无法理解我做错了什么并且完全停留在这一点上..

错误:

org.hibernate.procedure.NoSuchParameterException:找不到 使用该位置 [1] 注册的参数 org.hibernate.procedure.internal.ProcedureCallImpl.getParameterRegistration(ProcedureCallImpl.java:338) 在 org.hibernate.procedure.internal.ProcedureOutputsImpl.getOutputParameterValue(ProcedureOutputsImpl.java:68) 在 org.hibernate.jpa.internal.StoredProcedureQueryImpl.getOutputParameterValue(StoredProcedureQueryImpl.java:276)

实体类如下:

    @Entity
@NamedStoredProcedureQuery(name = "getCountries", procedureName = "spLT_Countries", resultClasses = Country.class)
public class Country implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "countryid")
    private Long id;

    @Column(name = "ISOCountrycode")
    private String iso2;

    @Column(name="Country")
    private String name;
        :
        :
}

MS SQL Server 中的存储过程,现有一个我无法修改。

ALTER procedure [dbo].[spLT_Countries]
as
begin
    SET NOCOUNT ON
    Set transaction isolation level read uncommitted
    Select countryid, ISOCountrycode, Country from RN_Country order by Country

end

任何帮助将不胜感激,因为我无法取得进一步的进展。

【问题讨论】:

  • 如何调用存储过程?
  • 我正在使用 JPA 注释 - @NamedStoredProcedureQuery(name = "getCountries", procedureName = "spLT_Countries", resultClasses = Country.class) 调用过程但映射参数失败
  • @NamedStoredProcedureQuery 只是声明查询。它不会调用它。

标签: hibernate spring-boot spring-data-jpa


【解决方案1】:

使用JPAEntityManager 来返回存储过程的结果,如下所示:

  javax.persistence.Query query=em.createNativeQuery("exec getCountries");  
  List<country> results = query.getResultList();

您可能需要从country 类中删除不必要的注释。

【讨论】:

  • 感谢 Farhang,这行得通,但我只想使用 JPA 注释而不是直接使用 EntityManager。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 2014-03-14
  • 2020-05-29
  • 2015-01-18
  • 2021-05-25
  • 2018-08-08
相关资源
最近更新 更多