The general concept of caching persistent objects is that when an object is first read from external storage, a copy of it will be stored in the cache. | Hibernate Tutorial 12 Caching Objects By Gary Mak hibernatetutorials@ September 2006 1. Level of caching The general concept of caching persistent objects is that when an object is first read from external storage a copy of it will be stored in the cache. For the subsequent readings of the same object it can be retrieved from the cache directly. Since caches are typically stored in memory or local disk it will be faster to read an object from cache than external storage. If using properly caching can greatly improve the performance of our application. As a high performance O R mapping framework Hibernate supports the caching of persistent objects at different levels. Suppose we get an object with same identifier for two times within a session will Hibernate query the database for two times Session session try Book bookl Book id Book book2 Book id finally If we inspect the SQL statements executed by Hibernate we will find that only one database query is made. That means Hibernate is caching our objects in the same session. This kind of caching is called first level caching whose caching scope is a session. But how about getting an object with same identifier for two times in two different sessions Session sessionl try Book bookl Book id finally Session session2 try Book book2 Book id finally Page 1 of 8 We will find that two database queries are made. That means Hibernate is not caching the persistent objects across different sessions by default. We need to turn on this second level caching whose caching scope is a session factory. 2. The second level caching To turn on the second level caching the first step is to choose a cache provider in the Hibernate configuration file . Hibernate supports several kinds of cache implementation such as