Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Hibernate- Caching

Hibernate provides us with three caching mechanism:

1. First Level Cache

2. Second Level Cache

3. Query Cache

First Level Cache

First Level Cache is the 'Session' object in Hibernate. It is a mandatory cache and the Session takes care of managing the Entity objects inside it.

Example :

Hibernate doesn't trigger multiple update statement and hits the database. But if there are multiple update statements in a session, Hibernate only triggers the last update statement. All this are managed inside the Session or First Level Cache.


Second Level Cache

There are instances when the First Level Cache is unable to handle some critical scenarios. In such cases Second Level Cache comes into picture.

It needs to be configured and there are few cache Providers listed below:

- EHCache

- OSCache

- JBoss Cache

We will be taking the example of EHCache and explain how 'Second Level Cache' works.

Below are the steps to download and setup the EHCache:

1. Go to the download folder where you have downloaded the Hibernate zip file. Then go to
  the path

hibernate-release-4.2.3.Final/lib/optional/ehcache

2. Take all the jar files from the above location and place them along with other jars in
  your project.

3. Now, in 'Hibernate.cfg.xml', enable the second level cache and set the necessary properties
  for 'EHCache'.



hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
 <session-factory>
    <property name="hibernate.dialect">
     org.hibernate.dialect.PostgreSQLDialect
    </property>
    <property name="hibernate.connection.driver_class">
     com.postgresql.Driver
    </property>
    <property name="hibernate.connection.url">
     jdbc:postgresql://localhost/newDb
    </property>
    <property name="hibernate.connection.username">
     myuser
    </property>
    <property name="hibernate.connection.password">
     Password123
    </property>
    <property name="hbm2ddl.auto">
     create
    </property>

    <property name="hibernate.cache.region.factory_class">
     org.hibernate.cache.ehcache.EHCacheRegionFactory
    </property>

    <property name="hibernate.cache.use_second_level_cache">
      true
    </property>

    <mapping resource="Employee.hbm.xml"/>

 </session-factory>
</hibernate-configuration>

4. In the Employee.hbm.xml mapping file, set the EHCache using the '<cache >' tag.


<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class name = "Employee" table = "EMPLOYEE">

   <cache usage="read-write">

   <id name = "id" type = "string">
     <column name = "ID">
    </id>
    <property name = "name" type = "string">
     <column name = "NAME">
    </property>
   </class>
</hibernate-mapping>