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

标签 java spring hibernate stored-procedures

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

相关文章:

Java MS Word 类应用程序

java - 复杂的Spring框架服务层

Spring Security 5.2 密码流程

java - 如何使用 Jackson 反序列化 2D 数组

java - Spring /hibernate : EntityManager multiplies instances

java - 连接 sql server JAR 文件

java - 从 Int 数组中删除重复(重复)元素

java - 处理超出锁等待超时的钩子(Hook)

java - hibernate HQL元素错误

java - Hibernate Spatial 5 - GeometryType