How to embed Oracle hints in Hibernate query
2015-09-16 11:18
585 查看
With Oracle hints, you can alter the Oracle execution plans to affect the way how Oracle retrieve the data from database.
In Hibernate, is this possible to embed the Oracle hint into the Hibernate query?
Hibernate
Can you embed the Oracle hint into HQL with Hibernate custom comment “
2. Try Hibernate
Enable the
Using Hibernate
Output
The correct way should be…
2. Hibernate will add an extra space in between “
In Hibernate, there are still no official way to embed the Oracle hints into Hibernate query langueges (HQL).
output
In Hibernate, is this possible to embed the Oracle hint into the Hibernate query?
Hibernate setComment()
?
Can you embed the Oracle hint into HQL with Hibernate custom comment “setComment()” function? Let’s see an example here
1. Original Hibernate Query
This is a simple select HQL to retrieve aStockwith a stock code.
String hql = "from Stock s where s.stockCode = :stockCode"; List result = session.createQuery(hql) .setString("stockCode", "7277") .list();
Output
Hibernate: select stock0_.STOCK_ID as STOCK1_0_, stock0_.STOCK_CODE as STOCK2_0_, stock0_.STOCK_NAME as STOCK3_0_ from mkyong.stock stock0_ where stock0_.STOCK_CODE=?
2. Try Hibernate setComment()
Enable the hibernate.use_sql_commentsin Hibernate’s configuration file (
hibernate.cfg.xml) in order to output the custom comment to your log file or console.
<!-- hibernate.cfg.xml --> <?xml version="1.0" encoding="utf-8"?> ... <hibernate-configuration> <session-factory> ... <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="use_sql_comments">true</property> <mapping class="com.mkyong.common.Stock" /> </session-factory> </hibernate-configuration>
Using Hibernate
setComment()to insert a custom comment to your query.
String hql = "from Stock s where s.stockCode = :stockCode"; List result = session.createQuery(hql) .setString("stockCode", "7277") .setComment("+ INDEX(stock idx_stock_code)") .list();
Output
Hibernate: /* + INDEX(stock idx_stock_code) */ select stock0_.STOCK_ID as STOCK1_0_, stock0_.STOCK_CODE as STOCK2_0_, stock0_.STOCK_NAME as STOCK3_0_ from mkyong.stock stock0_ where stock0_.STOCK_CODE=?
3. Is this work?
It’s not, there are two problem with Hibernate custom comments.1. The Oracle hint have to append after the ‘select’, not before.
Hibernate generated query/* + INDEX(stock idx_stock_code) */ select
The correct way should be…
select /*+ INDEX(stock idx_stock_code) */
2. Hibernate will add an extra space in between “/* +
” automatically.
In Hibernate, there are still no official way to embed the Oracle hints into Hibernate query langueges (HQL).Working solution
The only solution is using the HibernatecreateSQLQuerymethod to execute the native SQL statement.
String hql = "/*+ INDEX(stock idx_stock_code) */ select * from stock s where s.stock_code = :stockCode"; List result = session.createQuery(hql) .setString("stockCode", "7277") .list();
output
Hibernate: /*+ INDEX(stock idx_stock_code) */ select * from stock s where s.stock_code = ?
相关文章推荐
- 基于 Red Hat 的发行版 Oracle Linux 正式发布Oracle Linux 7.1
- Oracle Containers for J2EE远程安全漏洞(CVE-2014-0413)
- Oracle 10g R2不能使用EM的问题
- 表空间操作
- PreparedStatement中in子句的处理
- VMware下RedHat4.8_64位安装Oracle 10g RAC--简略脚本
- oracle sql日期比较
- 基于 Red Hat 的发行版 Oracle Linux 正式发布Oracle Linux 7.1
- OS block size和Oracle block size,查找OS Blocksize的方法
- oracle中创建数据库和表空间的几点总结
- 数据库自动备份脚本
- oracle的nvl函数的使用介绍
- 解决oracle用户连接失败的解决方法
- oracle的一些tips技巧
- Oracle 下的开发日积月累
- Oracle存储过程之数据库中获取数据实例
- Windows下ORACLE 10g完全卸载的方法分析
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- ORACLE LATERAL-SQL-INJECTION 个人见解