---
title: "Hibernate中调用存储过程"
author: "weibaojun"
date: "2011-01-10T17:21:18Z"
tags: []
categories: ["数据库"]
slug: "hibernate"
draft: false
lastmod: "2011-01-10T17:21:18Z"
id: 867436
url: "https://weibaojun.iteye.com/blog/867436"
---

在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();
```