Hibernate Overview and Architecture - Learn the basics of the Hibernate ORM framework
This article is providing the overview of the Hibernate framework and discussing about the Architecture of the Hibernate ORM.
Here we will discuss about Hibernate Architecture. Hibernate has a layered architecture making it easy for developers to select between core components and other components. Hibernate is used to develop data access layer of Java applications. It can work with applications based on both JSW and JEE.
Hibernate can be configure in two ways-using configuration file named hibernate.cfg.xml and by using *.hbm or Annotated POJO classes.
Hibernate uses POJO's and saves its to database table. It uses the configuration files and annotation to provide the metadata to the ORM system. You can easily use the persistent object (POJO) classes in your application and use Hibernate to persist it to the datbase. Meta-data is used to map the instance variable in the class with the columns in the database. Hibernate automatically generates SQL query to perform operations like select, insert, update and delete.
Java APIs like JDBC, JTA(Java Transaction API) and JNDI(Java Naming and Directory Interface) are used between Hibernate and database server.
The above diagram shows how hibernate works.
Here is how hibernate works (Hibernate Architecture explained):
To preserve data an Entity class mapped with database table is managed by Hibernate. At the same time an instance of SessionFactory is used to get the Session object which is the main interface between your application and database. Session factory implements Factory design and loads hibernate.cfg.xml file. Session object used to interact with the database through the methods of the interface.
Configuration class perform two operations-database connection and class mapping setup. hibernate.cfg.xml is used to configure the database connection. Class mapping creates the link between java classes and database tables.
SessionFactory creates Session instances to communicate with the database. Whenever application starts, SessionFactory object is created. It is a thread safe. There is one SessionFactory for each database. SessionFactory is created in configuration file.
Multiple SessionFactory objects are created when we need more than one database.
Hibernate Session is runtime interface between application and hibernate. Session object saves and retrieves persistent objects. You can create and destroy Session object as and when you need.
Caution: Session object is not thread safe, do not open it for long time.
Hibernate Transaction is single threaded object used for a unit of work with the database. Transaction manager handle your transactions.
Hibernate provide simple SQL, native query and HQL for performing different database operations. Instance of Query is found by Session.createQuery().
Hibernate Criteria is used to retrieve objects. Criteria objects performs database operations.
Components of Hibernate Architecture:
Hibernate Connection Management allows management of database, which requires lot of resources to open and close the database connection.
Hibernate Transaction Management allows user to execute several database statements at a time.
Object Relational Mapping (ORM): maps the data representation from an object model to a relational database.
Hibernate architecture is also known as Full Cream architecture when three components are used together and is called as Lite architecture if only ORM component is used.
Learn the basics of Hibernate Basics:
- Hibernate 4.2.7 Maven dependency - Learn how to use the Maven dependency in your project.
- Hibernate Overview - Learn the overview of Hibernate framework
- Hibernate Architecture - Learn the Architecture of Hibernate ORM Framework
- Setup Hibernate Environment - Learn How you can setup development environment using Hibernate ORM framework.
- Hibernate Configuration files - Learn about the Configuration files in the Hibernate framework
- First Hibernate Example Step by Step in Eclipse - Learn how to create first Hibernate example
- Hibernate Save Example - Learn how the save example works
- Hibernate Load Example - In this example you will learn how to load entity with the help of session.load() method
- Hibernate save or update method example - In this example you will learn how to use the saveOrUpdate() method.
- Hibernate Session.delete() Example - Learn how to delete the entity using Hibernate.