What is Hibernate? In common man’s language


Hibernate is an object-relational mapping (ORM) library for the Java language, providing a framework for mapping an object-oriented domain model to a traditional relational database. Hibernate solves object-relational impedance mismatch problems by replacing direct persistence-related database accesses with high-level object handling functions.

What you can do with it is, you can map every table to a representative class. So to read the table, you just invoke an object of the class and to update it with some fresh data, you just pass the data to the object and Hibernate will take care of storing it into the corresponding table. That is why it is referred to as “Object Relational Mapping”. And with Hibernate, you can do anything and everything that you do otherwise with a normal database.

Since Hibernate is all about hiding the tables and dealing purely with objects, it has evolved a query language that is suited to the “object”ive way of looking at things. This language is the HQL(hibernate query language).

Lets say you have a table “mobilephones” with the fields “modelno”,”modelname”,”companyname”,”price”.

Then while using Hibernate, you would have to create a class named MobilePhone. The class would look somewhat like this:-

class MobilePhone{

String modelNo;

String modelName;

String companyName;

Double price;

//all the get.. and set.. methods for the above variables.

//(these methods are a must, and they also make life simpler)

}

You would map this to the table “mobilephones” by giving the values in a Hibernate configuration file.(About that in another post).

Ok, table is setup, it contains data. We have also mapped it to a class whose object we can use. Now how do we fetch data?? In normal SQL console, we would type “select * from mobilephones” and hit enter. Hibernate Query Language(HQL) specifies a similar way of doing this. In Hibernate we use a “from” statement to achieve this. So I just have to say “from MobilePhone” in my query and pass it on to the Entity Manager, and it will execute “select * from mobilephones”.

Advertisements

About vatsalad

Hi, I'm Vatsala
This entry was posted in hibernate and tagged , , , . Bookmark the permalink.