gpt4 book ai didi

java - @NamedStoredProcedureQuery 不适用于 Hibernate 5.1.x 和 JPA2.1

转载 作者:行者123 更新时间:2023-11-30 06:34:12 25 4
gpt4 key购买 nike

在使用 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;
}
}

现在我尝试像这样调用存储过程:

    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

最佳答案

这是 Proc 上的权限问题,不幸的是,错误消息没有显示这一点,并且在这种情况下具有误导性。

关于java - @NamedStoredProcedureQuery 不适用于 Hibernate 5.1.x 和 JPA2.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45490197/

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