What is Hibernate

n this post, we will see about Hibernate and ORM. We will also see about the architecture of Hibernate.

Basic points about Hibernate.

  1. Hibernate is a framework that is used to communicate the java application to the database.
  2. Hibernate is an ORM tool. ORM stands for Object Relational Mapping which maps the object(entity) to data stored in the database.
  3. Hibernate was introduced in 2001 by Gavin King.  See more details about Hibernate’s history and release here.
  4. Hibernate is open source and currently maintained by JBoss. See official docs here.
  5. Hibernate provides a lot of features and benefits over JDBC.

Understanding ORM.

Consider we have one java class (Called Entity in terms of hibernate/JPA) Book.java and one table BOOK in the database. The table BOOK is mapped in entity with @Table annotation. Similarly, all column in the table Book is mapped with entity’s fields using @Column annotation. The mapping of Java classes(entity) with the table and entity’s field with the database’s column is called ORM.

In other words -ORM maps Java classes to the database table and Java data types(fields) to SQL data types(column).

What is Hibernate and ORM
What is Hibernate and ORM

In the above diagram, we have an entity called Student.java. We are using the @Coulumn annotation with id, name, rollNumber, and university fields. With @Column annotation, we are defining the database Book’s table column name. For example, we have id, name, roll_number, and university columns in the database.

The architecture of Hibernate.

What is Hibernate

Let’s see Hibernate component in detail. We will see Hibernate and ORM components and architecture in depth.

Configuration –  We provide hibernate database and details in the configuration file(for eg. hibernate.cfg.xml or hibernate.properties).  The Configuration class reads information from the configuration file (hibernate.cfg.xml or another configuration file) and create a Configuration object.

Configuration configuration = new Configuration().configure("hibernate.cfg.xml");

The Configuration object is used to create a SessionFactory reference.

SessionFactory – SessionFactory is an interface available in org.hibernate package which extends Referenceable and Serializable interface and provides factory methods to get session object. We can get Session Factory using the Configuration object as below.

Configuration configuration = new Configuration().configure("hibernate.cfg.xml");

SessionFactory sessionFactory = configuration.buildSessionFactory();

This sessionFactory object will have all the information that we have provided in hibernate.cfg.xml file. See more details about Session and SessionFactory here.

Session – Session is an interface available in org.hibernate package which provides different API to communicate java application to hibernate. We can get a Session reference using SessionFactory.

Session session = sessionFactory.openSession();

Transaction – Transaction is an interface available in or.hibernate package. We can get a Transaction reference using the session object. The transaction is an optional element.

Transaction tx = session.beginTransaction();

See the Spring @Transactional annotation example.

Persistent Objects  – The Entity or POJO are persistent objects. We need to configure these classes in hibernate.hbm.xml file or we can annotate that class with @Entity annotation.

ConnectionProvider – The ConnectionProvider is an interface available in org.hibernate.connection package.  ConnectionProvider is a way to obtain JDBC connections.

TransactionFactory – The  TransactionFactory interface available in org.hibernate package. It is an Optional element which is a factory for Transaction instances.

That’s all about what is Hibernate and ORM.

You may like.

References.

Configuration class docs.
SessionFactory interface docs.
Session interface docs.
Transaction interface docs.

Summary –  We have seen about Hibernate and ORM. We also cover the architecture of Hibernate.