Cache structure
You may not have realized but you have already been using at least the first-level cache that is managed by the Hibernate session. In addition to the first-level cache, Hibernate also offers the ability to set up a second-level cache. Furthermore, it's possible to cache the result of queries that are frequently executed, which are known as query caches. Let's discuss these here.
Cache scope
Before we discuss cache levels, we need to understand the scope of a cached entity:
A cached entity may only span the life of a transaction; this means the cached entity is only available for the thread that owns the persistence unit of work.
The next wider scope is the process, for example, your web application. An entity may be cached as long as the web application is running. In this case, the cached entity may be shared by multiple threads, perhaps even concurrently.
Finally, if you are in a clustered environment, entities can be cached on every node of the cluster. Clearly, when an entity...