Issue
I need some pojo objects across my application so I want to know how to enable Second Level Cache. Until now by default First Level cache is enabled, I would also like to know what advantages and disadvantages of Second Level cache there are.
Solution
This is what you need to do:
Set the following Hibernate properties:
<property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.provider_class">ehcache</property>
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" />
Define the Caching type for each entity:
@Entity @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public class MyEntity { ... }
Answered By - Vlad Mihalcea
Answer Checked By - Mary Flores (JavaFixing Volunteer)