gpt4 book ai didi

java - 如何在 spring hibernate 中使用@NamedNativeQuery 将参数传递给存储过程

转载 作者:行者123 更新时间:2023-11-30 08:22:00 25 4
gpt4 key购买 nike

我有一个带有参数@id的过程,我也在spring hibernate中调用@NamedNativeQuery,我想知道如何在@NamedNativeQuery的查询属性中传递过程中的参数

我的代码是这样的。

@NamedNativeQuery(name = "Callmyprocedure", query="{CALL sampleprocedure :id}", callable = true, resultClass = Subscriber.class)

其中 id 是我的过程参数,但它不起作用。

最佳答案

看看这个:

@Entity  
@NamedNativeQuery(name = "SampleNameQuery",query = "call spS_NamedQuery(?,?)",resultSetMapping="mapping",resultClass = NamedQuery.class)
@SqlResultSetMapping(name="mapping",columns=@ColumnResult(name="value"))
public class NamedQuery {

@Id
public String name;

@Column
public String value;
}
. . . .

然后你像这样传递参数:

 Transaction trx = null;  
Session session = HibernateSessionFactory.getSession();
try {
trx = session.beginTransaction();

org.hibernate.Query query = session.getNamedQuery("SampleNameQuery");
query.setParameter(0,"fsdfsdf");
String value = "";
query.setParameter(1,value);
List objList = query.list();
trx.commit();

希望对你有帮助。

关于java - 如何在 spring hibernate 中使用@NamedNativeQuery 将参数传递给存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24714288/

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