` Hibernate First Level Cache with Example | Java2Study.com

Hibernate First Level Cache with Example

Hibenate Caching will improoving the performance of application by reducing the database calls that means by reducing the database queries.

First level cache always associates with the session object.Hibernate uses first-level-cache by default. In this cache with in the session if we call same query for any number of times Hibernate will generate only one select operation.That Means If we call query for first time ,The data will be retrived from the database.next time if we fire same query the data will be loaded from the cache. First level cache useful for one user application.

Lets See an Example For Hibernate First Level Cache.

The Project Structure:

package com.practice;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

public class Hospital {
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "hospitalid")
	private int hospitalId;
	private String hospitalName;
	@Column(name = "address")
	private String address;
	public int getHospitalId() {
		return hospitalId;
	public void setHospitalId(int hospitalId) {
		this.hospitalId = hospitalId;
	public String getHospitalName() {
		return hospitalName;
	public void setHospitalName(String hospitalName) {
		this.hospitalName = hospitalName;
	public String getAddress() {
		return address;
	public void setAddress(String address) {
		this.address = address;



<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	    <!--  Database Properties -->
	<property name ="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
	<property name  ="hibernate.connection.url">jdbc:mysql://localhost:3306/balaDB</property>
	<property name = "hibernate.connection.username">root</property>
	<property name = "hibernate.connection.password">root123</property>
          <!-- Hibernate Properties -->
   <property name = "hibernate.hbm2ddl.auto">update</property>
   <property name = "hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
	<property name = "hibernate.show_sql">true</property>
	<property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
	<!--  mapping files -->
	<mapping class = "com.practice.Hospital"/>


package com.practice;

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

public class Test {

	 * @mankali
	public static void main(String[] args) {
      Configuration configuration = new Configuration();
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        Transaction transaction = session.beginTransaction();
          // first time query
            Object object = session.get(Hospital.class, 2);
            Hospital hospital  = (Hospital)object;
            System.out.println( hospital.getHospitalName());
            //second time query
            Object object1 = session.get(Hospital.class, 2);
            Hospital hospital1  = (Hospital)object;
            System.out.println( hospital1.getHospitalName());
            //Third Time query
            Object object2 = session.get(Hospital.class, 2);
            Hospital hospital2  = (Hospital)object;
            System.out.println( hospital1.getHospitalName());
        System.out.println("data retrived  successfully...");




SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Hibernate: select hospital0_.hospitalid as hospitalid0_0_, hospital0_.address as address0_0_, hospital0_.hospitalname as hospital3_0_0_ from hospital hospital0_ where hospital0_.hospitalid=?
data retrived  successfully...