Hibernate 2nd level cache with HazelCast, Spring Boot and Spring Data JPA
Published: 2021-08-16 20:01:39


This blog explains the process to implement Hibernate 2nd level cache with HazelCast, Spring Boot and Spring Data JPA. Source code and config files are uploaded to [**Github**](https://github.com/pavankjadda/Hazelcast-Hibernate2ndLevelCache-SpringBoot.git)


Technologies
============

1.  Spring Boot 2.2.x
2.  Spring Data JPA 2.2.x
3.  HazelCast 4.0
4.  Docker

Start HazelCast instance
========================

HazelCast instances are setup as Docker containers using the docker-compose file shown below.

docker-hazelcast.yml

Start the HazelCast instances and management center using the following command.

```
$ docker-compose -f <location of the docker-hazelcast.yml > up
```

HazelCast management center should up and running at [**http://localhost:8090/login.html**](http://localhost:8090/login.html)**.** Create a new account to see the management dashboard.

<img alt="" class="fd ej ef eo w" src="https://miro.medium.com/max/1400/1\*pvyhuVJZ-fQemc46LaBvAQ.png" width="700" height="402" srcSet="https://miro.medium.com/max/552/1\*pvyhuVJZ-fQemc46LaBvAQ.png 276w, https://miro.medium.com/max/1104/1\*pvyhuVJZ-fQemc46LaBvAQ.png 552w, https://miro.medium.com/max/1280/1\*pvyhuVJZ-fQemc46LaBvAQ.png 640w, https://miro.medium.com/max/1400/1\*pvyhuVJZ-fQemc46LaBvAQ.png 700w" sizes="700px" role="presentation"/>

Hazelcast default Dashboard

Click on **Add Cluster Config** to add newly created cluster config. Enter the cluster name as **dev**(default name) and replace **192.168.1.157** with your host IP Address as shown below.

<img alt="" class="fd ej ef eo w" src="https://miro.medium.com/max/1400/1\*sNTgBYEBaicK6mQzqAaitw.png" width="700" height="399" srcSet="https://miro.medium.com/max/552/1\*sNTgBYEBaicK6mQzqAaitw.png 276w, https://miro.medium.com/max/1104/1\*sNTgBYEBaicK6mQzqAaitw.png 552w, https://miro.medium.com/max/1280/1\*sNTgBYEBaicK6mQzqAaitw.png 640w, https://miro.medium.com/max/1400/1\*sNTgBYEBaicK6mQzqAaitw.png 700w" sizes="700px" role="presentation"/>

Add dev cluster config

Save the config and go to home page to see list of added clusters. Select **dev** cluster and you should see the following dashboard

<img alt="" class="fd ej ef eo w" src="https://miro.medium.com/max/1400/1\*nrZrDw032M8PHN-g7vznbw.png" width="700" height="418" srcSet="https://miro.medium.com/max/552/1\*nrZrDw032M8PHN-g7vznbw.png 276w, https://miro.medium.com/max/1104/1\*nrZrDw032M8PHN-g7vznbw.png 552w, https://miro.medium.com/max/1280/1\*nrZrDw032M8PHN-g7vznbw.png 640w, https://miro.medium.com/max/1400/1\*nrZrDw032M8PHN-g7vznbw.png 700w" sizes="700px" role="presentation"/>

Dev Dashboard

Spring Boot Application
=======================

1.  **HazelCastConfig** class defines **Config** bean needed to connect Spring Boot app to HazelCast cluster as shown below

HazelCastConfig.java

2\. The demo Spring Boot application has Employee domain object with EmployeeController and EmployeeRepository defined using Spring Data JPA. It uses H2 in memory data base to persist data

```
@Data  
@Entity  
@Cache(region = “employeeCache”, usage = CacheConcurrencyStrategy.READ\_WRITE)  
public class Employee  
{   
   @Id   
   private String empId;   
   private String empName;  
}
```

3\. The annotation **@Cache** creates **employeeCache** map in HazelCast cache and takes care of **CachePut** and **CacheEvict** operations. By default **findAll(), create, update, delete** method calls hit database and **findById()** method hits HazelCast cache.

4\. Run the application and go to [http://localhost:8080/api/v1/employee/find/all](http://localhost:8080/api/v1/employee/find/all) to see all the employees.

5\. Create new employee by hitting the link [http://localhost:8080/api/v1/employee/create](http://localhost:8080/api/v1/employee/create)

```
Hibernate Logs:  
Hibernate: select employee0\_.emp\_id as emp\_id1\_0\_0\_, employee0\_.emp\_name as emp\_name2\_0\_0\_ from employee employee0\_ where employee0\_.emp\_id=?Hibernate: insert into employee (emp\_name, emp\_id) values (?, ?)Hibernate: select employee0\_.emp\_id as emp\_id1\_0\_0\_, employee0\_.emp\_name as emp\_name2\_0\_0\_ from employee employee0\_ where employee0\_.emp\_id=?  

```

6\. If you look at hibernate logs, you should see **2 select statements** for 2 findAll() method calls and **1 insert statement** for findById() method call

5\. Now try to get employee information by providing the ID created in step 2. [**http://localhost:8080/api/v1/employee/find/<**](http://localhost:8080/api/v1/employee/find/emp1250719217)**your\_emp\_id>**. If you look at the hibernate logs, you should see nothing. That means Spring Boot returned the employee record from HazelCast cache without hitting the database.

7\. Look at the following employeeCache map from HazelCast management center, you should be see all the **Gets** and **Puts** to cache

![](https://miro.medium.com/max/2000/1*bm133j9-TyfNnNqMt_Wqug.png)

---

Source code is available in [Github](https://github.com/pavankjadda/Hazelcast-Hibernate2ndLevelCache-SpringBoot) for reference and Happy Coding