Hibernate First Level Cache Example

This Hibernate First Level Cache Example tutorial will help you understand about First Level Cache mechanism of Hibernate. Notice thant if you use Hibernate Cache correctly it will be very useful in increasing application performance. The idea of Hibernate cache is to reduce number of database queries, hence improving performance of the application.

Hibernate supports different types of Cache:

First Level Cache: Hibernate first level cache is associated with the Session object or is known as Session cache. It is enabled by default and a mandatory cache through which all requests must pass. Any object cached in a session will not be visible to other sessions and when the session is closed, all the cached objects of a session will also be lost. Beside, Hibernate also provides methods through which we can delete selected objects from the cache or clear the cache completely.

Second Level Cache: Hibernate Second Level cache is optional cache and we can enable it through configuration. Hibernate have not implemented the Second Level cache yet. But we can use Cache providers like EHCache or Infinispan, etc that implements this cache.

Query Cache is caching the combination of query and values provided as parameters as key, and list of identifiers of objects returned by query execution as values. Notice that Hibernate will load objects using cached identifiers from second level.

Other interesting posts you may like

Let’s begin discovery the Hibernate First Level Cache Examples
We will create java project by using Maven in Eclipse. This project is with directory structure like below

Hibernate First Level Cache Example

First example about Hibernate First Level Cache Example

This example will demonstrate two cases: getting same object with the same session and getting same object with each seperated sessions

Run above the program and we get the output like this:

As you can see how Hibernate first level cache works on the above program. We can understand that 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.
First level cache can have old values, as you can see above that we have put my program to sleep for 15 seconds then I update the value (name from David Pham to David Villa) in database but it is not reflected in the same session. But in other session, it get the updated value.

Second example about Hibernate First Level Cache Example
This example will demonstrate how to use evict method to remove object in session:

Run above main and we get the output like below

As you see the above program. Now we can use session evict() method to remove a single object from the first level cache.

Third Example about Hibernate First Level Cache Example

This example will demonstrate how to use clear method to remove cache object in session:

Run above main and we get the output like below:

Now, we can also use session clear() method to clear the cache or delete all the objects from the cache. If you want to check if an object is present in the hibernate cache or not you can use session contains() method.

Important note: While running bulk queries or batch updates, maybe you will get memory problem. The best way is clearing the cache at certain intervals because Hibernate cache all the objects into session first level cache.

That’s it on the Hibernate First Level Cache Example tutorial.

Download complete source code, please click link below

HibernateCacheExample.zip (76 downloads)

Leave a Comment

*

Please share it if you found this useful
Hide Buttons