Search code examples
hibernateredissecond-level-cache

Disabling Hibernate Second level cache Read Or Update for specific query


I have Redis as my second level cache for Hibernate. I enabled caching for an entity based on its ID. However, I have few queries I need to run based on other attributes such as name, age etc... in which case, my current implementation is still hitting the cache, missing it, updating the entity if anything changed. Since it is caching based on ID, it is missing it. I want to ignore going to Cache if it is not requested with ID. What is the way to do it?

Let me know if you need any additional information.


Solution

  • You can set CacheMode.IGNORE using Session.setCacheMode(CacheMode).

    This would ignore the second level cache to read the data from the DB.