`
weibaojun
  • 浏览: 97723 次
  • 性别: Icon_minigender_1
  • 来自: 火星
社区版块
存档分类
最新评论

Hibernate中调用存储过程

阅读更多

在Hibernate中调用存储过程有两种方式

1, 用Hibernate提供的session的得到数据库连接对象,用原生态存sql的形式实现存储过程的调用。

String call = "{call p_detail_project_sum(?,?)}";
//		
		// 两种方式实现hibernat 调用 存储过程
		// 1 用原生态的jdbc形式	
		CallableStatement stcmt;
		try {
			stcmt = this.getSession().connection().prepareCall(call);
			
			// 设置参数
			int index = 1;
			stcmt.setString(index ++, budgetId);
			stcmt.setString(index ++, projectId);
			
			// 执行查询
			stcmt.execute();
			
		} catch (Exception e) {
			e.printStackTrace();
		}

 

 

2,用Hibernate提供的Query接口执行存储过程

// 2 用hibernate 中的query 接口
		// 创建查询quyer
		Query query = this.getSession().createQuery(call);
		query.setString(index ++, budgetId);
		query.setString(index ++ , projectId);
		
		// 执行查询
		query.executeUpdate();

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics