ORM – what is Object Relational Mapping?
ORM or Object Relational Mapping is a system that implements the responsibility of mapping the Object to Relational Model. That means it is responsible to store Object Model data into Relational Model and further read the data from Relational Model into Object Model.
why Object Relational Mapping?
- ORM is solution for impedance mismatch.
- The advantages of using ORM and the core drawbacks of using JDBC.
The Object Oriented (Domain) model use classes whereas the relational database use tables. This creates a gap (The Impedance Mismatch). Due to the difference between the two different models, getting the data and associations from objects into relational table structure and vice versa requires a lot of tedious programming.
Loading and storing objects using a tabular relational database exposes us to 5 mismatch problems.
- data navigation
1. Granularity :
Granularity is the extent to which a system could be broken down into small parts. Lets take an example of Person details, we could broke down person details into two classes one is Person and another is Address for code reusability and code maintainability purpose. But assume that to store Person details in database there is only one table called Person.
Sometimes you will have an object model which has more classes than the number of corresponding tables in the database (we says the object model is more granular than the relational model). This is Granularity Mismatch between Object Model and Relational Model.
2. Inheritance or Subtypes :
Inheritance is a natural concept in object-oriented programming languages. However, RDBMSs do not define anything similar on the whole (yes some databases do have subtype support but it is completely non-standardized). This is Inheritance Mismatch between Object Model and Relational Model.
3. Identity :
A RDBMS defines exactly one notion of ‘sameness’: the primary key. Java, however, defines both object identity
MARKDOWN_HASHc6b534470a3eb981e01888f23f003db9MARKDOWN_HASH and object equality
MARKDOWN_HASHf50d3d0436f979f5bf732f7583b6432aMARKDOWN_HASH. This is Identity Mismatch between Object Model and Relational Model.
4. Associations :
Associations are represented as unidirectional references in Object Oriented languages whereas RDBMSs associations are bidirectional by using foreign keys. If you need bidirectional relationships in Java, you must define the association twice. Likewise, you cannot determine the multiplicity of a relationship by looking at the object domain model.
5. Data Navigation :
The way you access data in Java is fundamentally different than the way you do it in a relational database. In Java, you navigate from one association to an other walking the object network.
This is not an efficient way of retrieving data from a relational database. You typically want to minimize the number of SQL queries and thus load several entities via JOINs and select the targeted entities before you start walking the object network.
The solution for above problems is use ORM tool :
An Object Relational Mapping Tool provides a simple API for storing and retrieving Java objects directly to and from the relational database. ORM is technique that allows an application written in an object oriented language to deal with information as objects, rather than using database specific concepts such as Rows, Columns and Tables which facilitated by Object/Relational Mapper.
2. Advantages of ORM tools :
- Better System Architecture
- Reduce Coding Time
- Caching And Transactions
1. Better System Architecture :
A good ORM tool designed by very experienced software architects will implement effective design patterns that almost force you to use good programming practices in an application. This can help support a clean separation of concerns and independent development that allows parallel, simultaneous development of application layers. If you create a class library ORM-generated data access code, you can easily reuse the data objects in a variety of applications. This way, code reusability will increase. If use in this way there will be a cleaner separation of data access layer.
2. Reduce Coding Time :
Most of the time database access code spec is simple insert, update or delete. These are SQL statements sometimes are quite tedious to code, if you use JDBC it does not allow you to store objects directly to the database, you must convert the objects to a relational format. ORM tool helps here, by generating them on fly and there by saves a lot of time.
3. Caching and Transactions :
Most ORM tools such as hibernate come with features such as Caching and Transactions. These features, if chosen to hand code are not so easy to implement.