How to enable second level cache in Hibernate

This is what you need to do:

  1. Set the following Hibernate properties:

     <property name="hibernate.cache.use_second_level_cache">true</property>
     <property name="hibernate.cache.provider_class">ehcache</property>
    
  2. Add an ehcache.xml file in your classpath, containing the cache configuration entries:

     <cache name="com.mycompany.MyEntity"
        maxElementsInMemory="50"
        eternal="true"
        overflowToDisk="false"
        timeToIdleSeconds="600"
        timeToLiveSeconds="600"
        diskPersistent="false"
        memoryStoreEvictionPolicy="LRU"       
     />
    
  3. Define the Caching type for each entity:

     @Entity
     @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
     public class MyEntity {
             ...
     }