Caching is all about application performance optimization and it sits between your application and the database to avoid the number of database hits as many as possible to give a better performance for performance critical applications.
- The first-level cache -Associated with Session
- The second-level cache -Associated with Session-factory
- The query cache(2nd level same associated with session factory)
-- Across sessions in an Application
The 'second-level' cache exists as long as the session factory is alive. The second-level cache holds on to the 'data' for all properties and associations (and collections if requested) for individual entities that are marked to be cached.
For Disabling the second level of cache we have to made following change to hibernate configuration file.
- <!-- Disable the second-level cache -->
- <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
- <!-- Enable the second-level cache -->
- <property name="cache.use_second_level_cache">true</property>
- <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
- @Cacheable
- @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
- @Entity
- @Table(name="EMPLOYEE")
- public class Employee implements Serializable
- {
- <hibernate-mapping>
- <class name="Employee" table="EMPLOYEE">
- This class contains the student detail.
- <cache usage="read-only">
- <id column="ID" name="id" type="java.lang.Long">
- <generator class="native">
- </generator></id>
- <property column="Employee_NAME" name="employeeName" type="java.lang.String">
- <property column="ADDRESS" name="address" type="java.lang.String">
- <property column="Employee_Code" name="emp_code" type="java.lang.Long">
- </property></property></property></cache></class>
- </hibernate-mapping>