2. Hibernate Architecture

 

Hibernate Architecture 

The architecture of Hibernate is based on the following key components:



1. Hibernate Configuration:

   - The configuration file (`hibernate.cfg.xml`) is used to configure Hibernate properties such as database connection settings, dialect, mapping files, and other environment-specific settings.

   - Alternatively, you can configure Hibernate programmatically using the `Configuration` class.

The Configuration object provides two keys components −

Database Connection − This is handled through one or more configuration files supported by Hibernate. These files are hibernate.properties and hibernate.cfg.xml.

Class Mapping Setup − This component creates the connection between the Java classes and database tables.



2. SessionFactory:

    - SessionFactory is an Interface which is present in org.hibernate package and it is used to create Session Object.

   - The `SessionFactory` is a heavyweight. 

   - It is immutable and thread-safe in nature.

   - It is responsible for bootstrapping Hibernate, reading the configuration, and creating sessions.

   - It is generally instantiated once during the application's startup and used throughout the application's lifecycle.

[  how it's immutable and thread-safe:

- Immutable: Once the sessionFactory is initialized, its configuration and internal state cannot be modified. This prevents unexpected changes that could lead to errors or inconsistencies in the application.

- Thread-safe: Since the sessionFactory is immutable, multiple threads can safely access it concurrently without the risk of concurrent modification issues. Each thread will receive the same immutable instance, ensuring consistent behavior across threads. ]


3. Session:

    - Session in Hibernate represents a connection between Java application and the database. 

    - It's like a bridge that allows you to interact with the database to perform operations such as saving, updating, retrieving, or deleting data.

    - It is lightweight and not thread-safe, so each thread should have its own `Session`.

   - A session is used to perform database operations (CRUD - Create, Read, Update, Delete), and it acts as a first-level cache for loaded objects.


4. Transaction:

   - Transactions in Hibernate are managed through the `Transaction` interface.

    - Transaction object is used whenever we perform any operation and based upon that operation there is some change in database.

    - Transaction object is used to give the instruction to the database to make the changes that happen because of operation as a permanent by using commit() method.

   - A transaction represents a single atomic unit of work in Hibernate. 


5. Query Language (HQL):

    - Query is a mechanism used to retrieve data from a relational database using.

   - Hibernate Query Language (HQL) is an object-oriented query language that is similar to SQL but operates on objects rather than tables. 

    - It allows developers to express queries in terms of their Java domain model rather than in terms of the database structure.

    

6. Criteria 

- The ultimate goal of a criteria query is to retrieve persistent objects (entities) from the database.

- The Hibernate Session interface provides createCriteria() method, which can be used to create a Criteria object that returns instances of the persistence object's class when your application executes a criteria query.


- Mapping Files (Hibernate Mapping):

   - Hibernate uses XML or annotations to define the mapping between Java objects and database tables. This mapping information is provided in XML files (e.g., `hbm.xml` files) or using annotations directly in the Java classes.

Ex : 

1. XML Mapping

<hibernate-mapping package="com.example.model">

    <class name="Product" table="products">

        <id name="id" type="java.lang.Long">

            <column name="product_id"/>

            <generator class="increment"/>

        </id>

        <property name="name" type="string">

            <column name="product_name"/>

        </property>

  </class>

</hibernate-mapping>


2. Annotation

@Entity
public class Product {
    @Id
    private Long id;

}


- POJO (Plain Old Java Object) Classes:

   - These are simple Java classes that represent entities in the application domain. Hibernate maps these classes to database tables using the mapping information provided in the mapping files.








Comments

Popular posts from this blog

5. JPA Relational Mapping & Fetch Types & Cascadding

4. Data Fetching Methods & @Embeddable annotation in Hibernate

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