Object Relational Mapping – An introduction to Hibernate

Hibernate

What is Data Persistence?

It is a mechanism by which data stored in a file or database.  Generally, software program retrieves data from database and manipulates the same.  The changes made to data should be stored for later use.  So by Data Persistence we store the data in a database or file and enable the data to sustain even after program terminates, which can be used for later use.

Achieving Data Persistence is a great challenge.  We achieved through JDBC.  But JDBC has some limitations, especially when performing operations using complex join.

What is JDBC?

  • JAVA Database Connectivity is an Application Programming Interface (API), helps an application to connect and interact with the database.  We can connect to relational databases and perform database operations such as CRUD, directly from JAVA using JDBC API.
  • JDBC uses various JDBC drivers, which helps the JDBC on how to connect and interact with a particular type of database.

What are the limitations of JDBC?

  • Even for simple CRUD operations, JDBC requires developers to write extensive code.
  • Requires developers to write database specific SQL queries.  The SQL queries were hardcoded. It requires changes if any requirement change occurs.
  • Requires developers to convert JAVA object to relational format before we store the data in a database.
  • Requires developers to manually convert the result returned by the database to the JAVA objects, which represents business domain model.
  • JDBC code is not portable among various databases as it uses specific JDBC drivers that instruct JDBC how to connect and interact with that specific database.
  • Any changes in the underlying database, requires significant code changes.

So JDBC is not suitable for data intensive application, which does frequent update or retrieval of data objects.

 

Key Challenge:

Object Oriented Programming represents business domain as inter-related objects and each objects various properties to store data.  In contrast, Relational Data model represent data in tabular format composed of various rows and columns.  The mismatch in the representation of data in object-oriented programming and relational database model is called as Relational Impedance Mismatch.

More Factors that impede developers:

  • In an object-oriented programming, an Object may be equal or identical.  Objects are said to be Equal, if they contain same values. Objects are said to be identical if they hold the same reference or memory address of the actual object location and represent the same object.  In contrast, there is no separate notation for Identity and Equality in relational databases.
  • Object oriented programming support Inheritance, where as Relational databases do not. An Object inherits properties from a Parent Object class by means of Inheritance.  It was a great challenge to persist a very rich inheritance hierarchy of objects into the database.
  • Finally, object oriented Programming language supports Association, of course the Relational database as well.  Object oriented programming language supports many type of association such as one-to-one, one-to-many and many-to-many.  But Relational database supports using foreign key which is reference to the Primary key in another table. It does not support one-to-many or many-to-many associations.

What is Object Relational Mapping (ORM)?

ORM is a programming technique by which we overcome the challenge imposed by the mismatch representation of data between object oriented programming and relational database.  By this technique, we map a Class in object-oriented programming to a Table in Relational database.  So every instance of a class (object) represents rows in a Table, that is, each object represent one row in database.  Object properties represent columns in a database.  Once the classes in object-oriented programming mapped to tables in Relational database, we can directly persist an object into the database table easily.  No need to manually convert object into specific relational database format.

Advantages ORM:

  • Eliminates the need to convert objects into relational format.
  • Reduces coding effort highly.
  • Highly portable among various underlying databases.
  • Suitable for data-centric applications.
  • Direct query for an object from the underlying database. No need to write database specific SQL queries.

 

 

JAVA Persistence API (JPA) is a specification for ORM. It is an industry standard. It guides us to achieve data persistence in JAVA applications by mapping the persistence classes in object-oriented programming to the tables in relational database.  The mapping is achieved by means of separate XML file and distributed within application. 

Any ORM tool must comply with JPA specification and Hibernate framework does that.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: