Disable caching a specific query

I was trying to disable a specific query from being saved to Hibernate queryPlanCache and came across 2 methods in Query that seem to be there for this:

setCacheable(false) and setCacheMode(CacheMode.IGNORE)

However, using both of these in isolation and in combination did not yield the desired result.

I also tried setCacheMode(CacheMode.IGNORE) on Session object but that too failed.

Is there a way to achieve this?

Note: I do not want to disable the cache for all queries, but only for a specific one.

You can disable query plan caching for a specific query by calling org.hibernate.query.SelectionQuery#setQueryPlanCacheable or set the hint query.setHint( HibernateHints.HINT_QUERY_PLAN_CACHEABLE, false )

1 Like

We are using version 5.4. I guess HibernateHints got introduced later. I could not find it.
And mine is a deletion query. So I may not be abe to use SelectionQuery.
Please let me know if there is a different approach possible with earlier versions.

There is no possibility. You have to update to ORM 6.4 if you want this feature.

1 Like