Templates by BIGtheme NET

Object Relational Mapping Basics

In this article, I will give you introduction about What is ORM,
Why do we required some tool like ORM, pain points with traditional JDBC.
What best we can do with ORM and what are available ORM tools in market.

What is ORM?
– ORM stands for Object Relational Mapping, is a technique for converting data between
relational databases and object oriented programming languages.

What this ORM is doing? Simple answer is data conversion between objects and DB tables.

– Here the question is, Why this data conversion is required?
Simple answer is, In programming languages Objects are holding temporary data
to save in relational database as rows of a table, data conversion is required.

Here Class corresponds to Table and object corresponds to row of that particular table.
For example,
I have User class with 5 properties ID, NAME, ADDRESS, PHONE and DOB.
Corresponding Table USER with columns ID, NAME, ADDRESS, PHONE and DOB.

If we have 5 user objects like userObj1, userObj2, userObj3, userObj4 and userObj5.
All these objects values will be saved into USER table as rows.

userObj1 —-saved as —- first row in USER table.
userObj2 —-saved as —- second row in USER table.
userObj3 —-saved as —- third row in USER table.
userObj4 —-saved as —- fourth row in USER table.
userObj5 —-saved as —- fifth row in USER table.

The data conversion is required, because all these objects are not same as tables in relational data base.
May be similar, so data conversion is required always. All individual java entity needs to be converted
into individual entries called row.

Suppose, we have one user property isValidUser.
private boolean isValidUser;
There is no boolean data type in DB, we need to save this as CHAR(1) values are Y/N.
Then data conversion is required.

What are the pain points with traditional JDBC?
I have listed few pain points, while working with traditional database.
1) Mapping member variables to columns
We Need to map member variables of entity class to columns of database tables,
each and every time member variable that has to be persist.

2) Mapping relationships
There relations like 1-1, 1-M, M-1 and M-M. In this case we have to
write a code that handle all these relations.
Suppose user object is refer to address table, In that case the values providing to
the address column needs to validate against ADDRESS table.

3) Handling data types – This is a challenging task, because the object oriented language data types
and relational database data types are not same, most of the time required data conversion.
Suppose, think about boolean data type in OOP language, in DB there is no boolean data type.
This should be persist as CHAR data type with possible values Y (or) N.

Why do we required some tool like ORM?
Overcome all the pain points we required some ORM tool, that helps to do easy mapping.
Another important advantage is, we can develop database independent application.

There are many ORM tools are available in market,
Few of popular ones are,

1) Hibernate
2) Ibatis
3) JPA

*** Venkat – Happy leaning ****