Hibernate Caching - First Level Cache With Examples

Welcome to Hibernate Caching – First Level Cache Example Tutorial. Recently we looked into Hibernate architecture, hibernate mapping and how to use HQL for firing SQL queries in object oriented way. Today we will look into one of the important aspects of Hibernate – Hibernate Cache.

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:

  1. 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.
  2. 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.
  3. 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.


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.

First Level Cache in Hibernate Important Points

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

  1. Hibernate First Level cache is enabled by default, there are no configurations needed for this.
  2. 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.
  3. 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.
  4. We can use session evict() method to remove a single object from the hibernate first level cache.
  5. We can use session clear() method to clear the cache i.e delete all the objects from the cache.
  6. 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.
  7. 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.

By admin

Leave a Reply

%d bloggers like this: