zizoudev
zizoudev

Reputation: 21

How to add an Optimizer Hint to Hibernate criteria api query

I'm using Hibernate 4.3.7/jpa 2, I have noticed that from hibernate 4.3 i can embed Oracle query hints into hibernate query throw addQueryHint, i tried to fix it using the both criteria api and hql, but unfortunately it doesn't work for me !!

Is there any way to turn running this ? here is my code

public ListgetAll() {

    Session session = em.unwrap(org.hibernate.Session.class);
      Criteria criteria = session.createCriteria(Person.class).add(Restrictions.like("surname", "H%"));
      criteria.addQueryHint("ALL_ROWS");
      return criteria.list();

}

any response will be greatly appreciated

Thank you

Upvotes: 2

Views: 3637

Answers (1)

Stuart McIntyre
Stuart McIntyre

Reputation: 998

I haven't had any luck with addQueryHint. According to the documentation, it should pass the hints through, but when I look at the SQL sent it's not there. I have noticed the test for this functionality is using the Oracle dialect and we're using SQL Server so I thought it may be that it's not implemented in SQL Server, but I see you're having the problem in Oracle.

Here's is the description from the documentation:

Add a DB query hint to the SQL. These differ from JPA's QueryHint, which is specific to the JPA implementation and ignores DB vendor-specific hints. Instead, these are intended solely for the vendor-specific hints, such as Oracle's optimizers. Multiple query hints are supported; the Dialect will determine concatenation and placement.

Anyway, I have found a really awful hack/workaround that works but I would love to see addQueryHint fixed.

.add(Restrictions.sqlRestriction("1=1 ADD_YOUR_HINT_HERE")))

Upvotes: 3

Related Questions