@NamedStoredProcedureQuery无法使用Hibernate 5.1.x和JPA2.1

时间:2017-08-03 16:45:39

标签: java hibernate jpa

在使用Hibernate,JPA 2.1和Sql Server 2008R2时,我似乎无法解决一个奇怪的问题。希望有人能引导我走向正确的方向。我甚至不确定这是错误还是配置问题。

    @Entity
@NamedStoredProcedureQuery(
        name = "getProc", 
        procedureName = "proc_get_country_of",
        resultClasses = { ProcGetCountryOf.class }, 
        parameters = {
            @StoredProcedureParameter(name = "a_currency", type = String.class, mode = ParameterMode.IN),
            @StoredProcedureParameter(name = "b_currency",type = String.class, mode = ParameterMode.IN)
        })

public class ProcGetCountryOf implements java.io.Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -8655437017378046511L;
    @Id
    @Column(name = "country_of")
    private String country_of;

    public String getCountry_of() {
        return country_of;
    }

    public void setCountry_of(String country_of) {
        this.country_of = country_of;
    }
}

现在我试图像这样调用Store程序:

    try {

         EntityManagerFactory emf = Persistence.createEntityManagerFactory("mcm-jpa");
            EntityManager em = emf.createEntityManager();

            StoredProcedureQuery sp = em.createNamedStoredProcedureQuery("getProc")
                .setParameter("a_currency", "USD")
                .setParameter("b_currency", "CHF");
            sp.execute();

            ProcGetFxoCountryOf res = (ProcGetFxoCountryOf) sp.getSingleResult();
            System.out.println(res);

            em.close();
            emf.close();

    } catch (Exception e) {
        e.printStackTrace();
        return e.getMessage();
    }
    return "sucess";

不幸的是,这似乎不起作用,因为我得到以下异常:

[8/3/17 7:21:41:054 EDT] 00000d3a SystemOut     O   INFO  2017-08-03 07:21:41,054 HHH000204: Processing PersistenceUnitInfo [
        name: mcm-jpa
        ...]
[8/3/17 7:21:41:116 EDT] 00000d3a SystemOut     O   INFO  2017-08-03 07:21:41,115 HHH000412: Hibernate Core {5.1.7.Final}
[8/3/17 7:21:41:117 EDT] 00000d3a SystemOut     O   INFO  2017-08-03 07:21:41,117 HHH000206: hibernate.properties not found
[8/3/17 7:21:41:118 EDT] 00000d3a SystemOut     O   INFO  2017-08-03 07:21:41,118 HHH000021: Bytecode provider name : javassist
[8/3/17 7:21:41:149 EDT] 00000d3a SystemOut     O   INFO  2017-08-03 07:21:41,149 HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
[8/3/17 7:21:41:240 EDT] 00000d3a SystemOut     O   INFO  2017-08-03 07:21:41,240 HHH000400: Using dialect: org.hibernate.dialect.SQLServer2008Dialect
[8/3/17 7:21:41:780 EDT] 00000d3a SystemOut     O   INFO  2017-08-03 07:21:41,780 HHH000397: Using ASTQueryTranslatorFactory
[8/3/17 7:21:41:972 EDT] 00000d3a SystemOut     O   Hibernate: {call proc_get_fxo_country_of(?,?)}
[8/3/17 7:21:42:166 EDT] 00000d3a SystemOut     O   WARN  2017-08-03 07:21:42,166 SQL Error: 0, SQLState: S1093
[8/3/17 7:21:42:167 EDT] 00000d3a SystemOut     O   ERROR 2017-08-03 07:21:42,167 Parameter a_currency was not defined for stored procedure proc_get_fxo_country_of.

商店程序肯定具有以下属性:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE proc [dbo].[proc_get_fxo_country_of]

@a_currency varchar(3),
@b_currency varchar(3)

as
begin
...
end

1 个答案:

答案 0 :(得分:1)

这是关于Proc的权限问题,遗憾的是错误消息没有显示出来并且在这种情况下具有误导性。