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:
Liked it! Share this Tutorial