How to use Oracle query hint in Hibernate

无人久伴 提交于 2019-12-07 16:28:52

问题


I am trying to use Oracle hint in Hibernate to call force index, but didn't find any proper API in Hibernate 3.6.10.Final.

I somehow tried with projections in Hibernate criteria:

proList.add(Projections.sqlProjection("/*+ INDEX_DESC(CONTACT_USER_FK_I) */", new String[]{}, new Type[]{}));
                proList.add(Projections.property("objectId"));
                criteria.setProjection(proList);
                return criteria.list();

But I am getting the exception below:

    EXCEPTION
Caused by: 8 SQL Error  (could not execute query; SQL [select /*+ INDEX_DESC(CONTACT_USER_FK_I) */, this_.CONTACT_ID as y0_ from R4GDEV01_MBW.CONTACT this_ w
here this_.USER_ID=? and this_.ADDRESS_BOOK_ID in (?) and this_.DELETION_DATE is null order by lower(this_.FIRSTNAME) asc]; nested exception is org.hibernate
.exception.SQLGrammarException: could not execute query)
        at com.fusionone.pml.dao.hibernate.AbstractDao.executeCallback(AbstractDao.java:391)
        at com.fusionone.pml.dao.hibernate.AbstractContactDao.searchContacts(AbstractContactDao.java:1019)
        at com.fusionone.nab.core.service.impl.MergeServiceImpl.getFilteredContactIds(MergeServiceImpl.java:154)
        ... 91 more

I found out that the projection is appending a , after query hint.

Is there any other way to use Oracle query hint in Hibernate 3.6.10 criteria or in HQL?

Thanks


回答1:


You should try to search first. Here is a simple trick to solve this, just transform the hint into a column:

"/*+ INDEX_DESC(CONTACT_USER_FK_I) */ 1 as MYHINT"



回答2:


Your problem is that there is nothing previous to ,. Due to you want to add a projection on objectIdattribute, you could transform your criteria to somenthing like this:

Projections.sqlProjection("/*+ INDEX_DESC(CONTACT_USER_FK_I) */ objectId", new String[]{}, new Type[]{}))


来源:https://stackoverflow.com/questions/34544998/how-to-use-oracle-query-hint-in-hibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!