Pages Navigation Menu

Coding is much easier than you think

Introduction to Hibernate

In this to we will learn about what hibernate is, and how to use hibernate in our application and its features.

What is hibernate ?

  • It called as ORM tool (we will learn in subsequent tut)
  • Used in the data layer to save date into the database
  • Implements JPA (JPA is a set of standards which is prescribed for doing any operation in database.

Hibernet cover

1. Problem which Hibernate solves

Mapping member variables to column

Let take a Student class as an example, which has the following fields ID, Name, Department, Phone no, Address, Active.Hibrenet introductionIn a running application we may have lot of such objects, now to save the data in db; I need to create a Student table, in which I will save the Student objects as rows. So here N no of objects equal to N no of row in the table.

This implies that the class corresponds to the table and object of the class corresponds to the rows in the table. In java application, we will have a JDBC to connect to the db, and we will take this Student object, create an insert sql query and do an insert, Now all these data will be stored in db.

Now to select these data, we will create a select query to pull to the records and to store the records we will create a Student object to. So in Java we have data in form of objects, but there is no object in table, So java entity needs to be converted into a table entity, (i.e.) records.

The way we normally convert is by taking each of the values and map to its rows, this mapping is more tedious as we need to convert each object into SQL quires for saving, and for retrieving I have to convert a result set into corresponding java objects.

2. Mapping relationship

For example Student table has a column which is the primary key of another table (Address).
Hibernet example
Here in java side care should be taken to map Student object to Student table and Address object to Address table.

3. Handling data types


To check whether the Student is active or not, in java side, we will create a Boolean variable which hold the value true or false, here in case of Database, we cannot have a column with type Boolean, for this scenario we can either have a char type or int type. So now we have to handle this data type conversion by ourselves.

4. Managing the changes to object state.

Suppose if we want to update any of the field n DB, then we have to pull the object from db, store it in Student object then write a java code to trigger update query.

The above 4 are the most common problem which we face in most of the java application. In our next article we shall learn to set up Hibernate in eclipse.


About Mohaideen Jamil

Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.