Hibernate is some thing called as an ORM tool, however initially it started as a ORM tool there are lot of features that are added over the years, so that it is actually much more than ORM. ORM is in the data layer of an application, which is used to persist the application data into the database, it implements the JPA (Java Persistence API). JPA is a set of standards that follows the rules that have prescribed in the JPA specification.
Importance of hibernate:
Take an example of a simple Employee class in our application, it has 3 fields. In a running application we would have a lot of such objects in the memory, say I have 3 Employees in the application. I would have 3 Employee objects in my memory.
Now let me say I want to save this Employee data, so that I would have to use a database. 99 % of the time we would using the relation database because that is the most common database, at least as of today. We would have to persists all these Employee objects into the databases. Persistence is the another word for saving. How would I do that, I would have the employee table. And I would save the each Employee data in the form of rows in the employee table. I would have 3 columns that corresponds the properties in the Employee object. Each of the Employee data has to be saved in the employee table. Employee employee1 would be saved in the table as a row1 and Employee employee2 would be saved in the employee table as a row2 and as well. So this is some thing that, we have been doing over the years. We normally doing, when we come with Java application using the JDBC connection and we would connect with the database and then we would take this Employee object to persist.
Now I take this Employee object and I would get all these fields and would create sequel query to insert into the database. So depending on what the user information is, those data would go as a sequel query and it would be insert, and in the same way I would create a Employee object and I would have to do select on the employee table, once I do a select, I would get a result set. Now we have to parse the result set and get all the individual data sets and create a new Employee object and fill up with the resultant data using the setter methods.
So the problem here is that, we have objects in Java. But we do not have objects in the database level. The Employee object is individual entity that needs to be converted to table row as a individual entity. And the way we normally converted is by using some code which takes each of the Employee object properties, and takes each of the values of the member variable and maps it to sequel query. Now this mapping is the pain, we need to convert each object to a sequel query for saving and as well as retrieving. we would have to convert result set to an object for this we need to have another class for converting this functionality.
The pain points:
Mapping the member variables to columns :
we need to map the member variables to the columns. Each and every member variable can be persist has to be mapped manually.
Now lets say add a new column or add a new field in my object, now I want to make those changes in the code in order to map it I will hate to that particular lops where am I pulling up the records, I need to add these extra fields so that from the result set I will have to pull up this extra field. So any changes have to be done our selves, need to write our code in order to accommodate the changes. Even we didn’t change any thing we need to write the code for mapping the variables.
Now we saw user object mapped with the user table. Let”s say user object has a reference to another address object. Now how it would work, we probably have address table in that case I would have to map the user object to the user table and I have to map the address object to the address table and if there is a dependency between the two objects I would have to create a foreign key and then it is mapped to address table, primary key would be mapped as a foreign key of the user table. This relationship also have to be taken care of.
Handling Data Types:
Let’s say I have a boolean in my user object say it is active user. Now I want to track whether a particular user is active or not. In order to do that I would declared it as a boolean and I will give a value true or false. Now how would I do that in the database. Most of the databases do not have boolean types. It would probably be a char. So that I can save “Y” or “N” or an integer it might has 0 in the case of false or 1 in the case true. But those are not really boolean but I will have to handle this database conversion my self when Iam actually writing the code to save the data or retrieve the data. So this manual handling is problem because if your making changes you have to go to that peace of code and make the change in that code.
Managing changes to the object state:
Now I pull up user information, I want userId 1 to 3, I pull it from data base. I have result set and then I will create an object and I pull all the data in to that object and I have the object ready. Now I passed it to the by business logic can change this object.like change the user email or last name as well. Now how would I manage this change, I will have to do again manually and execute the procedure to make this change to trigger and update query and what would be updated again we would have to frame a sql query as sets. And depending on the value that has changed we will have to update the changes in the database in order to accommodate the changes. So again this some thing that we need to handle ourselves.
These are the common pain points in any application.
So whats happened is many times because of this problems you would normally have an object that maps to a table so I have 5 tables so I have 5 Objects and most of the times I have this happened a lot we would not worry too much about establishing object relationships. Lets take the example of an Employee and address . Now the Employee has a particular address, Employee row in the Employee table has a corresponding address row in the address table. And it has been mapped by a foreign key now ideally if I have to represent an object I would have an address object and then I have to associated with the user object but what I would do is since its a pain to established this relationships depending on the primary key and foreign key I would treat a separate objects and Iam establishing a association based on the key itself even in the Java layers.
This is a big problem and this is some thing that many people have addressed over the years and there have been solutions which handle the solution elegant ways of handling this solution. Ideally the solution that addresses this problem will help us to map the objects with the relational database. Hence the name of what the solution is trying to do is called Object Relational Mapping so this is ORM. And this is the primary reason why hibernate even came into existence in order to help the users to map such a thing and it is a framework which addresses this among things and the primary use is to map this gap between objects and relational database.