landdin0 2013-08-03
最近公司的DBA整天在烦,大家都很清楚在烦些什么的了。数据库的性能问题。。总是跑过来跟我说,hk hibernate 可不可以加上一些oracle的特性hints啊?我说可以啊。。。。。。。。。。。下面的不说了,哈。下面来看具体hibernate 使用hints.
在hibernate中,怎么样使用hints?
hibernate 在创建Query时,其中有一个方法叫setComment(),我们就可以通过这个方法去开启hibernate 对 oracle的hints特性。具体例子
1.原生的hql
String hql = "from Stock s where s.stockCode = :stockCode"; List result = session.createQuery(hql) .setString("stockCode", "7277") .list();
执行时输出下面的sql
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.使用hibernate setcomment()
开启hibernate use_sql_comment的功能在配置文件里修改
<!-- 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>
下面使用hibernate hints的例子
String hql = "from Stock s where s.stockCode = :stockCode"; List result = session.createQuery(hql) .setString("stockCode", "7277") .setComment("+ INDEX(stock idx_stock_code)") .list();
输出下面sql
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=?
本文章来自:http://blog.mkfree.com/posts/51f86d7c479eef767b508aad