How to call stored procedure in Hibernate
2016-05-04 10:04
357 查看
http://www.mkyong.com/hibernate/how-to-call-store-procedure-in-hibernate/
In this tutorial, you will learn how to call a store procedure in Hibernate.
SQL
In MySQL, you can simple call it with a call keyword :
SQL
Java
Java
Call it with getNamedQuery().
Java
Markup
Call it with getNamedQuery().
Java
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.SQL
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 :
SQL
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.Java
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.Java
//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().
Java
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.Markup
<!-- 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().
Java
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.相关文章推荐
- 多线程环境下对变量的读写操作的原子性问题【baidu】
- Jindent——让intellij idea 像eclipse一样生成模版化的javadoc注释
- 快速理解RxJava源码的设计理念
- 转:手册网(程序员开发手册相关网站)
- Class类工具 - ClassUtils.java
- Win10系统电脑发不了短信提示"无可用手机"的解决方法
- Android提示版本更新
- MediaPlayer和SoundPool
- 对象或库文件“*.lib”是使用比创建其他......的解决方法
- ASP.NET 2.0中的数据操作之九:跨页面的主/从报表
- poj2486 Apple Tree 树上分组背包
- Android中两种设置全屏的方法!!!
- Spark:一个高效的分布式计算系统
- [Mac] Atom 酷炫插件安装 activate-power-mode
- 浅谈域名发散与域名收敛
- LeetCode 207. Course Schedule(课程安排)
- Json工具类 - JsonUtils.java
- Retrofit2 完全解析 探索与okhttp之间的关系
- Sql Server知识点系统化Study
- android 之ProgressDialog进度条 显示 单位(kb)