Don't Sleep on Hibernate

A quick look into one of the best ORM (Object-Relational Mapping) Frameworks in Java

Hibernate

What is Hibernate?

As mentioned, Hibernate is an Object-Relational Mapping Framework that can be used for Java.

It is an open-source framework which provides abstraction, so that the programmer does not need to manually register, establish, open and close database connections each time they are required. It is a framework with the power to automatically implement those functions, making the job of the programmer simpler.

When data is stored in a database, there is a persistence logic that is used to add that data into the relevant database. 

In normal cases, this persistence logic is written by the programmer, but with the help of a framework such as Hibernate, that is no longer required. It has the power to develop the required persistence logic in order to create connections to the database and perform necessary CRUD functions.

Further, the persistence logic that is created is database software independent.

Hibernate is the middle man between the Java code and the database.


What is Object-Relational Mapping (ORM)?

When creating a Java project, there will arise a need to connect that project to a database. It is at this point that connectors are required to connect the code with the database.

If you are using a MySQL database, there will be a need to write SQL queries in order to create the tables that are required by the project.

Writing all these codes and queries can be a time-consuming and daunting task.

That is where ORM (Object-Relational Mapping) Frameworks come in handy. These frameworks have the capability to define the tables and data that are required by the classes that are within the project code.

It makes it possible for the programmer to create objects, which are generally used for temporary storage, and execute a command as simple as saying save(object); in order to save the data within the object into a database.

Requirement for Hibernate

  1. Unlike its alternative (JDBC), Hibernate is not dependent upon the Database that is being used. In JDBC, the persistence logic depends of the database and has to be changed if a change in databases is required.
  2. JDBC does not support object-oriented relationships.
  3. JDBC code is not portable.
  4. There is always a standard code that must be written in order to establish and create the database connection, which lengthens and complicated the code.
  5. Exception handling in JDBC is mandatory, meaning that try-catch statements must be used in order to properly execute queries.

Advantages of Hibernate

  1. Hibernate is a free, open-source framework.
  2. Persistence logic (SQL code) is generated automatically.
  3. Reduces time taken to develop.
  4. Makes the code database software independent.
  5. Changes in the database can be made easily. Only one file would need to be changed.

Comments

Popular Posts