How to call stored procedure in Hibernate
2015-10-22 17:25
316 查看
In this tutorial, you will learn how to call a store procedure in Hibernate.
In MySQL, you can simple call it with a
1. Native SQL –
You can use
2.
Declare your store procedure inside the
Call it with
Call it with
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
callkeyword :
CALL GetStocks('7277');
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 @NamedNativeQueriesannotation.
//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()); }
Conclusion
The above three approaches are doing the same thing, call a store procedure in database. There are not much big different between the three approaches, which method you choose is depend on your personal prefer.相关文章推荐
- jquery中把字符串转换为数字
- Windows Server 2003 下配置 MySQL 群集(Cluster)
- iOS中的沙盒路径
- MySQL RESET MASTER与RESET SLAVE
- js Table鼠标滑过变色,单击变色,隐藏某列的方法
- 从AndroidStudio的启动参数开始,了解一下JVM的一些东西(内存使用,JIT等)
- Proguard mapping文件的理解及Decode 堆栈信息
- grunt使用
- Android 多媒体播放器Creat过程
- forkingdog 和 masonry 实现自动计算行高
- POJ 3614
- GetImageSize用法
- ORACLE函数大全
- 手贱随手在Linux敲了 as 命令,出不来了
- C语言:编写从一个字符串中提取一个子字符串的函数my_substr
- 数据结构与算法--字符串
- 动态代理
- CSS:透明度 opacity与rgba()的区别
- Java学习笔记(十三):关于子类初始化的过程
- socket的发送与接收缓冲区