使用Spring JPA调用StoredProcedure会导致错误

时间:2015-01-27 00:39:23

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

我正在使用hibernate 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

任何帮助都会受到高度赞赏,因为我无法取得进一步的进展。

1 个答案:

答案 0 :(得分:0)

使用EntityManager的{​​{1}}返回存储过程的结果,如下所示:

JPA

您可能需要从 javax.persistence.Query query=em.createNativeQuery("exec getCountries"); List<country> results = query.getResultList(); 类中删除不必要的注释。