Search code examples
oraclehibernateormhibernate-mappinghibernate-criteria

How to use Oracle query hint in Hibernate


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


Solution

  • 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"