我有一个带有参数@id的过程,我也在Spring休眠状态下调用@NamedNativeQuery,我想知道如何在@NamedNativeQuery的query属性中的过程中传递参数

我的代码看起来像这样。

@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();


希望对您有所帮助。

07-24 18:37
查看更多