Second Level Cache

Click to rate this post!
[Total: 0 Average: 0]

Hibernate Second Level Cache:

Second Level Cache utilizes a typical reserve for all the session question of a session industrial facility. It is valuable on the off chance that you have various session objects from a session factory. Session Factory holds the second level store information. It is worldwide for all the session questions and not empowered of course.

Different sellers have given the execution of Second Level Cache:

  1. EH Cache
  2. OS Cachen
  3. Swarm Cache
  4. JBoss Cache

Different cache usage functionality

  • Read-only
  • Nonstrict-read-write
  • Read-write
  • Transaction

<h2>JAVA Tutorial Video</h2>



<cache usage=”read-only” />


Implementation read-only nonstrict-read-write read-write transactional
EH Cache Yes Yes Yes No
OS Cache Yes Yes Yes No
Swarm Cache Yes Yes No No
JBoss Cache No No No Yes

3 extra steps for second level cache example using EH cache:

1) Add 2 configuration setting in hibernate.cfg.xml file

<property name=”cache.provider_class”>org.hibernate.cache.EhCacheProvider</property>

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

2) Add cache usage setting in hbm file

        <cache usage=”read-only” />

3) Create ehcache.xml file

        <?xml version=”1.0″?>






 Hibernate and Struts 2 Integration:

We can coordinate any struts application with hibernate. There is no prerequisite of additional endeavors. In this illustration, we going to utilize struts 2 structure with hibernate. You need jostle documents for struts 2 and rest.

Example of Hibernate and struts2 integration
  • index.jsp
  •  user.hbm.xml
  •  hibernate.cfg.xml
  • struts.xml
  • welcome.jsp
  • web.xml

Hibernate and Spring Integration:

We can basically incorporate hibernate application with spring application. In hibernate structure , we give all the database data hibernate.cfg.xml document. Be that as it may, in the event that we will coordinate the hibernate application with spring; we don’t have to make the hibernate.cfg.xml record. We can give all the data in the application Context.xml record.

 Advantage of Spring framework with hibernate:

The Spring structure gives Hibernate Template class, so you don’t have to take after such a variety of steps like make Configuration, BuildSessionFactory, Session, starting and conferring exchange.

Using the Problem without spring :

//creating configuration

Configuration cfg=new Configuration();


//creating seession factory object

SessionFactory factory=cfg.buildSessionFactory();

//creating session object

Session session=factory.openSession();

//creating transaction object

Transaction t=session.beginTransaction();

Employee e1=new Employee(111,”arun”,40000);

session.persist(e1);//persisting the object

t.commit();//transaction is commited


Answer by using Template class of Spring :

Employee e1=new Employee(111,”arun”,40000);;

Methods of HibernateTemplate class:

No. Method
1) void persist(Object entity)
2) Serializable save(Object entity)
3) void saveOrUpdate(Object entity)
4) void update(Object entity)
5) void delete(Object entity)
6) Object get(Class entityClass, Serializable id)
7) Object load(Class entityClass, Serializable id)
8) List loadAll(Class entityClass)


  1. create table in the database
  2.  create applicationContext.xml file
  3.  create file
  4. create employee.hbm.xml file
  5.  create file
  6. create file

1) create the table in the database









package com.javaspot;

public class Employee {

private int id;

private String name;

private float salary;

//getters and setters


3) employee.hbm.xml

<?xml version=’1.0′ encoding=’UTF-8′?>

<!DOCTYPE hibernate-mapping PUBLIC

“-//Hibernate/Hibernate Mapping DTD 3.0//EN”



<class name=”com.javatpoint.Employee” table=”emp558″>

<id name=”id”>

<generator class=”assigned”></generator>


<property name=”name”></property>

<property name=”salary”></property>




package com.javaspot;

import org.springframework.orm.hibernate3.HibernateTemplate;

import java.util.*;

public class EmployeeDao {

HibernateTemplate template;

public void setTemplate(HibernateTemplate template) {

this.template = template;


//method to save employee

public void saveEmployee(Employee e){;


//method to update employee

public void updateEmployee(Employee e){



//method to delete employee

public void deleteEmployee(Employee e){



//method to return one employee of given id

public Employee getById(int id){

Employee e=(Employee)template.get(Employee.class,id);

return e;


//method to return all employees

public List<Employee> getEmployees(){

List<Employee> list=new ArrayList<Employee>();


return list;



5) applicationContext.xml:

In this record, we are giving every one of the informations of the database in the BasicDataSource protest. This question is utilized as a part of the LocalSessionFactoryBean class protest, containing some different informations, for example, mappingResources and hibernate Properties. The protest of LocalSessionFactoryBean class is utilized as a part of the Hibernate Template class.How about we see the code of applicationContext.xml document.

Hibernate Template Class

File: applicationContext.xml

<?xml version=”1.0″ encoding=”UTF-8″?>






<bean id=”dataSource” class=”org.apache.commons.dbcp.BasicDataSource”>

<property name=”driverClassName”  value=”oracle.jdbc.driver.OracleDriver”></property>

<property name=”url” value=”jdbc:oracle:thin:


<property name=”username” value=”system”></property>

<property name=”password” value=”oracle”></property>


<bean id=”mysessionFactory”  class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>

<property name=”dataSource” ref=”dataSource”></property>                   <property name=”mappingResources”>





<property name=”hibernateProperties”>


<prop key=”hibernate.dialect”>org.hibernate.dialect.Oracle9Dialect</prop>

<prop key=””>update</prop>

<prop key=”hibernate.show_sql”>true</prop>




<bean id=”template” class=”org.springframework.orm.hibernate3.HibernateTemplate”>      <property name=”sessionFactory” ref=”mysessionFactory”></property>


<bean id=”d” class=”com.javaspot.EmployeeDao”>

<property name=”template” ref=”template”></property>




package com.javaspot;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.beans.factory.xml.XmlBeanFactory;



public class InsertTest {

public static void main(String[] args) {

Resource r=new ClassPathResource(“applicationContext.xml”);

BeanFactory factory=new XmlBeanFactory(r);

EmployeeDao dao=(EmployeeDao)factory.getBean(“d”);

Employee e=new Employee();







Enabling automatic table creation, showing sql queries

 <property name=”hibernateProperties”>


<prop key=”hibernate.dialect”>org.hibernate.dialect.Oracle9Dialect</prop>

<prop key=””>update</prop>

<prop key=”hibernate.show_sql”>true</prop>


to our newsletter

Drop Us A Query

Trending Courses
  • Selenium with python
    Selenium with Python Training
  • As we know, that Selenium with Python Web Browser Selenium Automation is Gaining Popularity Day by Day. So many Frameworks and Tools Have arisen to get Services to Developers.

  • machine learning with python
    Machine Learning with Python Training
  • Over last few years, Big Data and analysis have come up, with Exponential and modified Direction of Business. That operate Python, emerged with a fast and strong Contender for going with Predictive Analysis.

  • Data science with R
    Data Science With R Training
  • Understanding and using Linear, non-linear regression Models and Classifying techniques for stats analysis. Hypothesis testing sample methods, to get business decisions.

  • data science with python
    Data Science with Python Training
  • Everyone starts Somewhere, first you learn basics of Every Scripting concept. Here you need complete Introduction to Data Science python libraries Concepts.

  • devops with azure
    Devops with Azure Training
  • As we Know Azure DevOps is a Bunch of Services, in guiding Developers. It contains CI/CD, pipelines, code Repositories, Visual Reporting Tools and more code management with version control.

  • python training
    Python Certification Training
  • Python is a dynamic interrupted language which is used in wide varieties of applications. It is very interactive object oriented and high-level programming language.

100% Secure Payments. All major credit & debit cards accepted.

Call Now Button