Hibernate Caching – First Level Cache Example Tutorial

In this tutorial, we explore the powerful Hibernate caching mechanism, focusing on the First Level Cache. Learn how Hibernate optimizes database interactions, reduces query execution times, and improves application performance. We’ll dive into practical examples, examine key concepts, and highlight important points to help you make the most of Hibernate caching in your projects.

Hibernate Caching

Hibernate Cache can be very useful in gaining fast application performance if used correctly. The idea behind cache is to reduce the number of database queries, hence reducing the throughput time of the application. Hibernate comes with different types of Cache:

First Level Cache

Hibernate first level cache is associated with the Session object. Hibernate first level cache is enabled by default and there is no way to disable it. However hibernate provides methods through which we can delete selected objects from the cache or clear the cache completely. Any object cached in a session will not be visible to other sessions and when the session is closed, all the cached objects will also be lost.

Second Level Cache

Hibernate Second Level cache is disabled by default but we can enable it through configuration. Currently EHCache and Infinispan provides implementation for Hibernate Second level cache and we can use them. We will look into this in the next tutorial for hibernate caching.

Query Cache

Hibernate can also cache result set of a query. Hibernate Query Cache doesn’t cache the state of the actual entities in the cache; it caches only identifier values and results of value type. So it should always be used in conjunction with the second-level cache.

Hibernate Caching – First Level Cache Example

For my hibernate first level cache example program, I am using same configuration as in HQL Example, you can check that out and configure the tables and populate it with dummy data. Let’s first look into the program, it’s output and then we will go through some of the important points related to Hibernate First Level Cache.


package com.journaldev.hibernate.main;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.journaldev.hibernate.model.Employee;
import com.journaldev.hibernate.util.HibernateUtil;

public class HibernateCacheExample {

    public static void main(String[] args) throws InterruptedException {
        
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        Transaction tx = session.beginTransaction();
        
        //Get employee with id=1
        Employee emp = (Employee) session.load(Employee.class, new Long(1));
        printData(emp,1);
        
        //waiting for sometime to change the data in backend
        Thread.sleep(10000);
        
        //Fetch same data again, check logs that no query fired
        Employee emp1 = (Employee) session.load(Employee.class, new Long(1));
        printData(emp1,2);
        
        //Create new session
        Session newSession = sessionFactory.openSession();
        //Get employee with id=1, notice the logs for query
        Employee emp2 = (Employee) newSession.load(Employee.class, new Long(1));
        printData(emp2,3);
        
        //START: evict example to remove specific object from hibernate first level cache
        //Get employee with id=2, first time hence query in logs
        Employee emp3 = (Employee) session.load(Employee.class, new Long(2));
        printData(emp3,4);
        
        //evict the employee object with id=1
        session.evict(emp);
        System.out.println("Session Contains Employee with id=1?"+session.contains(emp));

        //since object is removed from first level cache, you will see query in logs
        Employee emp4 = (Employee) session.load(Employee.class, new Long(1));
        printData(emp4,5);
        
        //this object is still present, so you won't see query in logs
        Employee emp5 = (Employee) session.load(Employee.class, new Long(2));
        printData(emp5,6);
        //END: evict example
        
        //START: clear example to remove everything from first level cache
        session.clear();
        Employee emp6 = (Employee) session.load(Employee.class, new Long(1));
        printData(emp6,7);
        Employee emp7 = (Employee) session.load(Employee.class, new Long(2));
        printData(emp7,8);
        
        System.out.println("Session Contains Employee with id=2?"+session.contains(emp7));
        
        tx.commit();
        sessionFactory.close();
    }

    private static void printData(Employee emp, int count) {
        System.out.println(count+":: Name="+emp.getName()+", Zipcode="+emp.getAddress().getZipcode());
    }
}

When we run above example, the output contains a lot of hibernate related information. But we are mostly interested in our code specific output and the queries fired by hibernate to load the data. The output snippet looks like below.


Hibernate Configuration loaded
Hibernate serviceRegistry created
Hibernate: select employee0_.emp_id as emp_id1_1_0_, employee0_.emp_name as emp_name2_1_0_, employee0_.emp_salary as emp_sala3_1_0_, address1_.emp_id as emp_id1_0_1_, address1_.address_line1 as address_2_0_1_, address1_.city as city3_0_1_, address1_.zipcode as zipcode4_0_1_ from EMPLOYEE employee0_ left outer join ADDRESS address1_ on employee0_.emp_id=address1_.emp_id where employee0_.emp_id=?
1:: Name=Pankaj, Zipcode=95129
2:: Name=Pankaj, Zipcode=95129
Hibernate: select employee0_.emp_id as emp_id1_1_0_, employee0_.emp_name as emp_name2_1_0_, employee0_.emp_salary as emp_sala3_1_0_, address1_.emp_id as emp_id1_0_1_, address1_.address_line1 as address_2_0_1_, address1_.city as city3_0_1_, address1_.zipcode as zipcode4_0_1_ from EMPLOYEE employee0_ left outer join ADDRESS address1_ on employee0_.emp_id=address1_.emp_id where employee0_.emp_id=?
3:: Name=PankajK, Zipcode=95129

First Level Cache in Hibernate Important Points

Important Points about First level cache in Hibernate that can be derived from above program are:

  • Hibernate First Level cache is enabled by default, there are no configurations needed for this.
  • Hibernate first level cache is session specific, that’s why when we are getting the same data in same session there is no query fired whereas in other session query is fired to load the data.
  • Hibernate first level cache can have old values, as you can see above that I have put my program to sleep for 10 seconds and in that time I updated the value (name from Pankaj to PankajK) in database but it didn’t get reflected in the same session. But in other session, we got the updated value.
  • We can use session evict() method to remove a single object from the hibernate first level cache.
  • We can use session clear() method to clear the cache i.e delete all the objects from the cache.
  • We can use session contains() method to check if an object is present in the hibernate cache or not, if the object is found in cache, it returns true or else it returns false.
  • Since hibernate cache all the objects into session first level cache, while running bulk queries or batch updates it’s necessary to clear the cache at certain intervals to avoid memory issues.

That’s all for hibernate caching and first level cache example, in future posts we will look into Hibernate Second Level Cache – EHCache implementation.

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in:

centron Managed Cloud Hosting in Deutschland

How to Calculate BLEU Score in Python?

Python
How to Calculate BLEU Score in Python? BLEU score in Python is a metric that measures the goodness of Machine Translation models. Though originally it was designed for only translation…