Search code examples
springspring-boothibernatecachinghibernate-cache

Is putting @Cacheable or @Cache above the Entity enough for Hibernate to start caching?


Is it enough and Hibernate checks methods automatically that are doing stuff with that entity and updates the cache? Or @Cacheable, @CachePut, @CacheEvict annotations above methods are mandatory for a working cache.


Solution

  • What is cached depends on the configuration. If you enable selective caching, you need to annotate all entities with @Cacheable that should be cached. For queries, you additionally need to provide a cache hint, but all of that can be read in the official documentation: https://docs.jboss.org/hibernate/orm/5.5/userguide/html_single/Hibernate_User_Guide.html#caching