6. Hibernate Query Language (HQL) & Caching in Hibernate

 

Hibernate Query Language (HQL)

Hibernate Query Language (HQL) is a powerful and flexible way to interact with a relational database using Hibernate, which is a popular Object-Relational Mapping (ORM) framework in Java. 


1. HQL - Hibernate Query Language:

   - HQL is like SQL, but it's designed to work with your Java objects, not directly with the database tables. It allows you to write queries using the names of your Java classes and properties, rather than database tables and columns.


2. Database Interaction - In traditional database operations, you use SQL (Structured Query Language) to interact with the database. SQL is a language that is specific to the database you are using (e.g., MySQL, PostgreSQL, Oracle).


3. Object-Relational Mapping (ORM): Hibernate helps you work with databases in a more object-oriented way. Instead of thinking in terms of database tables and SQL queries, you can work with Java objects that map to your database tables.


Caching in Hibernate 

Hibernate store the result in a cache. The next time you need the same data, Hibernate checks the cache first. If the data is there, it saves the time and resources required to fetch it from the database again.

This caching mechanism helps improve performance by reducing the number of database queries and making your application respond faster.


Caching in Hibernate:


1. What is Caching?

   - Caching is a mechanism that stores data in a temporary storage area, allowing future requests for that data to be served faster.


2. Why Use Caching in Hibernate?

    - Using caching in Hibernate can significantly improve the performance of an application by reducing the number of database round-trips and improving response times.

   - In Hibernate, caching helps improve performance by reducing the need to repeatedly fetch data from the database.

   - It stores the results of database queries, so if the same query is executed again, Hibernate can return the result from the cache instead of going to the database.


3. Types of Caching in Hibernate:

   - a. First Level Cache:

      - This is also known as the session-level cache.

      - Session Object hold the first-lelel cache data.

      - It exists within the scope of a single Hibernate session.

      - It is enabled by default and helps avoid redundant database calls within the same session.

      - Session object holds the first level chache data. The first level cache data will not be available to entire application.


   - b. Second Level Cache:

      - This is a shared cache that spans multiple sessions.

      - SessionFactory Object hold the Second-lelel cache data.

      - It is not enabled by default and needs to be configured.

      - It helps in scenarios where the same data is accessed by different sessions, reducing database load.

      - To use the second level cache, you need to configure a caching provider, such as Ehcache or Infinispan, and annotate entities or collections with @Cacheable and @Cache annotations.



- Cache Provider

Hibernate does not provide its own caching implementation; instead, it relies on third-party cache providers. These cache providers are responsible for managing the caching infrastructure and storing cached data. Different cache providers may offer different features and performance characteristics.


- EH (Easy Hibernate ) Cache

- Swarm Cache

        - Os Cache

        - JBoss Cache

- Infinispan

- Hazelcast



 c. Query Level Cache

Hibernate also implements a cache for the resulsets that integrates closely with the second-level cache.

Hibernate also supports caching query results to avoid re-executing the same query multiple times.

You can enable the query cache by setting the hibernate.cache.use_query_cache property to true.

Additionally, annotate queries or entities with @org.hibernate.annotations.QueryHints to specify caching options.


Hibernare Configuration File 

<!-- Enable query cache -->

<property name="hibernate.cache.use_query_cache">true</property>


<!-- Specify the caching provider for the query cache -->

<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>




Comments

Popular posts from this blog

5. JPA Relational Mapping & Fetch Types & Cascadding

4. Data Fetching Methods & @Embeddable annotation in Hibernate