我有一个带有参数@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/