What are the different levels of caching supported in Hibernate?
What are the different levels of caching supported in Hibernate?
Hibernate supports two main levels of cache:
1. First-level Cache – This is the default cache associated with the Session object. Hibernate uses this cache on a per-transaction basis. Mainly it reduces the number of SQL queries it needs to generate within a given transaction. Instead of updating after every modification in the transaction, it updates only at the end of the transaction.
2. Second-level Cache – This cache is associated with the SessionFactory object. To reduce database traffic, second level cache keeps loaded objects with the SessionFactory between transactions. These objects are not bound to a single user, available at the application level. Since the objects are available at the cache, each time a query returns an object, one or more database transactions can be avoided.
There is one more cache – Query level Cache is supported and this is more for caching query results which can be used along with second level cache for improved performance.