Before understanding Hibernate, let’s understand about ORM.
What is ORM ?
ORM stands for Object Relational Mapping
In any Java application, we will have objects which are representing the data that we store in the Database.
To store the data in the database, we need tables to be created in the DB and these tables are also called relational entities.
If we want to store these objects into database tables directly without writing any SQL, we need to map the class with the tables so that each object of that class can be mapped to each row in that table.
This kind of mapping between object and tables is called Object Relational Mapping(ORM).
So in ORM
class in java is referred to table in DB.
Instance of class also called objects referred to rows in the database table.
Each attribute in the class refers to each column in the table.
What is Hibernate?
Hibernate internally uses JDBC API to interact with the database
Hibernate is an open source and lightweight ORM and persistence framework which allows us to map plain old java objects to relational database tables.
It facilitates the storage and retrieval of java domain objects via Object Relational Mapping.
Hibernate is created by Gavin King
Hibernate is a lightweight or non-invasive framework because it does not force the programmers to extend any classes or to implement any interfaces.
Need for Hibernate?
Hibernate has come up with many benefits over JDBC which insists us to use it.
Let’s see them with the below conversation.
Student: Master, I have understood what is Hibernate, Thanks for that, but my question is why do we need to use Hibernate when we already have JDBC to connect to the database ?
Hey, no doubt we have JDBC to connect to database but in JDBC you need to write sql query to talk to DB right ?
For that you need to remember the syntax , need to write each column in the query and also you should not do any typo in your sql query.
What if I tell you, stop writing sql query in java, just save your object directly in DB without even writing a query.
How do you feel in this case?
Student:Oh very nice Sir,I don’t have to remember any syntax at all,great!!
but how my object gets saved to DB if I am not writing any query, do I need to write any configuration code for that ?
Master : yes, you need to write configuration code but that’s very simple, just need to write xml code only once for each class and table mapping, it increases the productivity of development.
Student: great sir,any other benifit of using Hibernate ?
Master: yes,it makes our application more portable with the database we use.
Student:Really!! How , can you please explain sir ?
Master:Assume we are using MYSQL database in our project, then we will write a JDBC code according to MYSQL in our application,but what if client says , we will be using Oracle database from next year ?
Then we need to change lot of code in our application to change JDBC code written specific to MYSQL which is no longer valid for Oracle.
If we use Hibernate, we just need to change Hibernate configuration file.
How simple is this?
Student: I am impressed with this Sir but I want to know more benefits about Hibernate.
Master: Sure , there are lot many benefits ,it makes developers to write very less code related to persistence.
They don’t have to follow 5 steps of JDBC connection process, no need to worry about closing a connection object so that they can completely focus on business logic. It helps us to maintain the code easily, so maintainability of code matters right?
Student: Great sir,I hope there are many other benefits in Hibernate if I am not wrong.
Master: yes, there are many other benefits like lazy loading, caching and other benefits which I will explain you later in detail.
Student: Thank you very much sir, now on I suggest my team to use Hibernate, it’s a wonderfull ORM framework.