How To Call Stored Procedure In Hibernate

In this tutorial, you will learn how to call a store procedure in Hibernate.

MySQL store procedure

Here’s a MySQL store procedure, which accept a stock code parameter and return the related stock data.

DELIMITER $$

CREATE PROCEDURE `GetStocks`(int_stockcode VARCHAR(20))
BEGIN
SELECT * FROM stock WHERE stock_code = int_stockcode;
END $$ DELIMITER ;

In MySQL, you can simple call it with a call keyword :

CALL GetStocks('');

Hibernate call store procedure

In Hibernate, there are three approaches to call a database store procedure.

1. Native SQL – createSQLQuery

You can use createSQLQuery() to call a store procedure directly.

Query query = session.createSQLQuery(
"CALL GetStocks(:stockCode)")
.addEntity(Stock.class)
.setParameter("stockCode", "7277"); List result = query.list();
for(int i=0; i<result.size(); i++){
Stock stock = (Stock)result.get(i);
System.out.println(stock.getStockCode());
}

2. NamedNativeQuery in annotation

Declare your store procedure inside the @NamedNativeQueries annotation.

 
//Stock.java
...
@NamedNativeQueries({
@NamedNativeQuery(
name = "callStockStoreProcedure",
query = "CALL GetStocks(:stockCode)",
resultClass = Stock.class
)
})
@Entity
@Table(name = "stock")
public class Stock implements java.io.Serializable {
...

Call it with getNamedQuery().

Query query = session.getNamedQuery("callStockStoreProcedure")
.setParameter("stockCode", "7277");
List result = query.list();
for(int i=0; i<result.size(); i++){
Stock stock = (Stock)result.get(i);
System.out.println(stock.getStockCode());
}

3. sql-query in XML mapping file

Declare your store procedure inside the “sql-query” tag.

<!-- Stock.hbm.xml -->
...
<hibernate-mapping>
<class name="com.mkyong.common.Stock" table="stock" ...>
<id name="stockId" type="java.lang.Integer">
<column name="STOCK_ID" />
<generator class="identity" />
</id>
<property name="stockCode" type="string">
<column name="STOCK_CODE" length="10" not-null="true" unique="true" />
</property>
...
</class> <sql-query name="callStockStoreProcedure">
<return alias="stock" class="com.mkyong.common.Stock"/>
<![CDATA[CALL GetStocks(:stockCode)]]>
</sql-query> </hibernate-mapping>

Call it with getNamedQuery().

Query query = session.getNamedQuery("callStockStoreProcedure")
.setParameter("stockCode", "7277");
List result = query.list();
for(int i=0; i<result.size(); i++){
Stock stock = (Stock)result.get(i);
System.out.println(stock.getStockCode());
}
 

How To Call Stored Procedure In Hibernate的更多相关文章

  1. SQL Server 在多个数据库中创建同一个存储过程(Create Same Stored Procedure in All Databases)

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 遇到的问题(Problems) 实现代码(SQL Codes) 方法一:拼接SQL: 方法二: ...

  2. Stored Procedure 里的 WITH RECOMPILE 到底是干麻的?

    在 SQL Server 创建或修改「存储过程(stored procedure)」时,可加上 WITH RECOMPILE 选项,但多数文档或书籍都写得语焉不详,或只解释为「每次执行此存储过程时,都 ...

  3. [转]Dynamic SQL & Stored Procedure Usage in T-SQL

    转自:http://www.sqlusa.com/bestpractices/training/scripts/dynamicsql/ Dynamic SQL & Stored Procedu ...

  4. [原] XAF How to bind a stored procedure to a ListView in XAF

    First, I suggest that you review the following topic to learn how to show a custom set of objects in ...

  5. Retrieving Out Params From a Stored Procedure With Python

    http://www.rodneyoliver.com/blog/2013/08/08/retrieving-out-params-from-a-stored-procedure-with-pytho ...

  6. Modify a Stored Procedure using SQL Server Management Studio

    In Object Explorer, connect to an instance of Database Engine and then expand that instance. Expand  ...

  7. Difference between Stored Procedure and Function in SQL Server

    Stored Procedures are pre-compile objects which are compiled for first time and its compiled format ...

  8. Oracle Stored Procedure demo

    1.how to find invalid status stored procedure and recompile them? SELECT OBJECT_NAME , status FROM u ...

  9. JDBC连接执行 MySQL 存储过程报权限错误:User does not have access to metadata required to determine stored procedure parameter types. If rights can not be granted,

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...

随机推荐

  1. 春&风

    流光飞舞中,消逝的背影连同流动的人群,早已荒凉. 谁是谁的谁?落入尘世间,只是光阴下面的一粒尘埃. 那些时光,那些爱,渐行渐远. 留在心底,淡淡的思念,如轻风一阵, 吹过 彼此的容颜.

  2. jmeter接口测试之登录测试

    注册登录_登陆接口文档 1.登录 请求地址: POST   xxxxxx/Home/Login 请求参数: args={ LoginName:"mtest", // 登录名,可以为 ...

  3. 【转】Android应用程序的数据存放目录解说

    Android的每个应用程序,都有自己的可控的目录. 在Setting/Application info里面,可以看到每个应用程序,都有Clear data和Clear cache选项. 具体这些目录 ...

  4. SqlBulkCopy 数据批量操作使用的类

    private void SqlBulkCopyByDataTable(string connectionString,string TableName,DataTable dt) { using ( ...

  5. 那些好用的iOS开发工具

    版权说明 本文首发于<程序员>杂志 2014 年 6 月刊,未经允许,请勿转载. 前言 从 苹果发明 iPhone 起,AppStore 上的一个又一个类似 flappy bird 的一夜 ...

  6. php 首页定时生成静态页面

    往往首页的js,商务通代码加的太多,导致页面访问速度变慢,可以把首页有动态变为静态进行访问,访问速度会有所提升,不过如果更新首页数据,并不能及时更新,而是你规定的时间内固定更新一次 代码如下: < ...

  7. js 刷新页面自动回到顶部

    <script type="application/x-javascript"> addEventListener("load", function ...

  8. Linux - 重定向与管道

    标准输出重定向 ">" 操作符:覆盖目标文件内容 huey@huey-K42JE:~/huey/linux/cmdline$ date > foo huey@huey- ...

  9. sparkSQL1.1入门

    http://blog.csdn.net/book_mmicky/article/details/39288715 2014年9月11日,Spark1.1.0忽然之间发布.笔者立即下载.编译.部署了S ...

  10. ACM——第几天

    第几天 时间限制(普通/Java) : 1000 MS/ 3000 MS          运行内存限制 : 65536 KByte总提交 : 1830            测试通过 : 525 描 ...