Hibernate Caching

Hibernate use caching to maintain the performance of an application. The caching lie between the application and the database to avoid the number of hits to database, it provide better performance for critical applications.
Hibernate caching

There are 3 type of cache use by hibernate:
  • First-level cache
  • Second-level cache
  • Query-level cache

First-level Cache

  • The first-level cache is the Session cache and is a mandatory cache through which all requests must pass. The Session object keeps an object under its own power before committing it to the database.
  • If you issue multiple updates to an object, Hibernate tries to delay doing the update as long as possible to reduce the number of update SQL statements issued. If you close the session, all the objects being cached are lost and either persisted or updated in the database.

Second-level Cache

  • Second level cache is an optional cache and first-level cache will always be consulted before any attempt is made to locate an object in the second-level cache. The second-level cache can be configured on a per-class and per-collection basis and mainly responsible for caching objects across sessions.
  • Any third-party cache can be used with Hibernate. An