What is Hibernate and ORM

In this post, we will see about Hibernate and ORM. we will also see about architecture of Hibernate.

Basic points about Hibernate.

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

We are looking into Hibernate and ORM in depth.

Understanding ORM.

Suppose 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 table Book is mapped with entity 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

 

The architecture of Hibernate.

architecture of Hibernate

 

Let’s see Hibernate component in details. We will see Hibernate and ORM component 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();

Session – Session is an interface available in org.hibernate package which provides different API to communicate java application to hibernate. We can get 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();

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.

Top