Topic 10- Caching in hibernate

image 2

Play Store Application link – Hibernate – in 10 steps – Apps on Google Play

 

hibernate cache

Cache definition-

Cache is a temporary data which is used to speed up the performance.

Types of caching in hibernate-

Caching LevelDefault OnSession-SpecificQuery Results CachedObjects EvictedConcurrent AccessUsageCode Snippets
First-level CacheYesYesNoYesThread-safeStoring and retrieving entities within a single session//Loading an entity from the first-level cache
Session session = sessionFactory.openSession();
Employee employee = session.get(Employee.class, 1);
Second-level CacheNoNoNoConfigurableConfigurabletoring and retrieving entities across multiple sessions//Enabling second-level cache
true

//Configuring second-level cache provider
org.hibernate.cache.ehcache.EhCacheRegionFactory
Query CacheNoNoYesConfigurableConfigurableCaching the results of a query//Enabling query cache
true

//Using query cache
Query query = session.createQuery(“from Employee”);
query.setCacheable(true);
List employees = query.list();

In the table above:

  • Caching Level : The level of caching, whether it’s first-level, second-level, or query cache.
  • Default On : Whether the cache is enabled by default or not.
  • Session-Specific : Whether the cache is specific to a single session or not.
  • Query Results Cached : Whether the cache stores the results of a query or not.
  • Objects Evicted : Whether the cache automatically evicts stale objects or not.
  • Concurrent Access : The level of concurrent access support provided by the cache.
  • Usage : The use case for which the cache is best suited.
  • Code Snippets : Examples of how to use the cache in code.

1-Session Cache/First level cache in hibernate-

  • First level cache is enabled by default.
  • First level cache can’t be disabled.
  • Objects are retrieved from current session.
    Example-
// Open a session
Session session = HibernateUtil.getSessionFactory().openSession();

// Begin a transaction
session.beginTransaction();

// Retrieve a customer object from the database
Customer customer = session.get(Customer.class, 1L);

// Print the customer's name
System.out.println(customer.getName());

// Retrieve the same customer object again
Customer customer2 = session.get(Customer.class, 1L);

// Print the customer's name again
System.out.println(customer2.getName());

// Commit the transaction
session.getTransaction().commit();

// Close the session
session.close();

In the above example, the first call to session.get(Customer.class, 1L) retrieves the customer object from the database and stores it in the first-level cache associated with the current session. The second call to session.get(Customer.class, 1L) retrieves the customer object from the first-level cache instead of hitting the database again.

It’s worth noting that the first-level cache is associated with a session, so if you open a new session, it will not have access to the objects stored in the first-level cache of the previous session.

Also, it’s important to note that hibernate’s first-level cache is enabled by default, so there is no need to do anything extra to enable it.

2- SessionFactory Cache/Second level cache in hibernate-

  • Second level cache is disabled by default.
  • Second level cache is configured to use.
  • Objects can be retrieved from various sessions.

Extra-Query Level cache in hibernate-

  • Query Cache is used to cache the results of a query. 
  • Stepes to enable query level cache-
    step 1- Only add this line to configuration.
    i.e.  <property name=”hibernate.cache.use_query_cache”>true</property>

Github project link for all 3 Cache in hibernate – https://github.com/kuldeep101990/cacheInHibernate