Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Hibernate: Story of Caching

DZone 's Guide to

Hibernate: Story of Caching

Caching is important for performance, so we take a look at how it works with an ORM like Hibernate.

· Performance Zone ·
Free Resource

Hi, all!

In this article, we are going to talk about caching in Hibernate.

Hibernate

Hibernate is an ORM Framework for Java. It is a framework for mapping an Object-Oriented Model to an Relational Model. Hibernate is concerned with data persistence as it applies to relational databases (via JDBC). It performs powerful object-relational mapping and query databases using HQL and SQL. It can cut down a lot of complexity and, thus, defects as well from your application, which may otherwise find a way to exist. We can also say it is a boon for developers with limited knowledge of SQL.

Caching

Caching is a mechanism to store data in a buffer memory (also known as cache) so that future request can be served faster. It helps in reducing the number of interaction to the database and enhance the performance of a system.

Why Is Catching Important?

  • Enhance the performance of database applications.
  • Reduces traffic between your application and the database
  • Access database if data is not available in the cache.

Types of Cache

First-Level Cache
Second-Level Cache
Query Cache

In this blog, we will focus on first-level cache. We will talk about second level and query cache in the next blog.

First-Level Cache

First level cache is a session cache. It is a session-scoped cache, which ensures that each entity instance is loaded only once in the persistent context. It is enabled by default and there is no way to disable it. As it is a session-scoped cache, all concurrent session work in isolation. Once the session is closed, its first-level cache is terminated as well.

Working

Whenever an application tries to load an entity it searched in the first-level cache, if it is not able to find a first-level cache, it hits the database and executes the query and stores it in the first-level cache and returns the result to the application.

Some Important Methods to Control the First-Level Cache

The first-level cache is the default cache in Hibernate, and there is no way to disable it. But, Hibernate provides some methods to control it.

 evict(Object object):

It removes an object from the session cache. After removing the object from the session, any change to object will not be persisted.

 clear():

When this method called, the first-level cache for that session is cleared, as well as all the objects from the cache.

 contains(Object object):

This method can be used to check that the given instance is associated with this session.

In the next blog, we will look at the second-level cache. Stay tuned!

References

https://docs.jboss.org/hibernate/core/3.5/api/org/hibernate/Session.html
Topics:
hibernate ,performance ,caching ,java ,first-level cache ,first level

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}