Object Relational Impedance Mismatch - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Sunday, December 10, 2017

Object Relational Impedance Mismatch

Object oriented language such as java represent data as interconnected graph of objects where as relational database management system represents data in table like format. Java implements the object model and relational database management systems implements the relational model because both the model are quite different to each other in the way they represent the data. When we load or store graph of objects using a relational database we come across five mismatch problem and this mismatch between the object model and relational model is called object relational impedance mismatch or paradigm mismatch.

There are mainly five mismatch between object model and relational mismatch.

Granularity Mismatch
Granularity mismatch is extent to which a system could be broken down into small parts or a component in an application. You have many granular object at various level of granularity. For example in an e-commerce application a person is a coarse grain object because it contain lot of details about the person such as personal address, order placed by him, and his billing address and thing like that. So person object could be broken down into many small parts or components.
Another granular object in an e-commerce application is address which is fine grained object because it contains only address specific details such as street name, city, state, country and things specific to an address. Coarse grain object consist of various fine grained objects or objects with fine granularity so in an object model you can have object with various level of granularity, but in relational model granularity is limited to two levels; tables and columns. so sometimes when you want data of objects in java stored in a relational database the way you designed table in database to store data will have more classes in object model than the number of corresponding tables in the database. For example here we have two classes. Person class and the address class associate with each other but we have only one table in the database to store data for it. So when we talk about java object and database table the granularity mismatch problem means storing objects that can have various level of granularity to tables and columns which are limited in granularity which means that the object model is more granular that relational model.
Subtype mismatch or Inheritance mismatch
The second granular mismatch we come across is subtype mismatch or inheritance mismatch. We have concept of subtype or inheritance in the object model but we have not any such things in relational model. In the example here book is a subtype of product, in the object model . but no such thing could be defined in relation model.
Identity mismatch or Sameness Mismatch
The third mismatch is Identity mismatch. In a relational database the sameness of two entities is defined only by primary keys. If the same values for their primary key they are considered identical or same but java defined both object identity and object equality. In above diagram object identity is being compared here which means results in false because id of foo object is 1 and id of bar object is 2. The value of id here reflects the address of an object in memory and object equality is being checked like foo.equals(bar) here which one result in either true or false based on how the equals method is implemented in the class instance of foo and bar referring too.
Associations mismatch
The fourth mismatch we come across is associations mismatch. Associations in object model is achieved by object references where as in relational model we use a foreign key to associate two entities and object references are bi-directional. If an association between objects should be navigable in both direction you must define the association twice. Once in each of the associated classes just like be the example here. But foreign key associations are not directional.
Navigation mismatch
The way to navigate data in java is fundamentally different than the way you do it in a relational database. In java you navigate from one association to another walking through the object graph. What this means is that to find a particular item you navigate from one object to another object following the associations between the object until you reach that item. For example to get the value of y here you first access the bar object then only you get value y. similarly If you want to get value of z object here you first excess the bar object and then zoo object using the associations between bar and zoo object. Once you have zoo object you could have access the value of z.
But It is not an efficient way of retrieving data from relation database. The single most importance thing to do to improve the performance of data access code to minimize the number of request to the database and the most obvious way to do that is to minimize the number sql query to the database. If you want to get data from more than one tables you write a SQL join query to join those tables and then get the data from the join tables.
For example here we have two tables the customer table and the order table if you want to get data from name column of the customer table and the data of order_no column of order table. You first join the customer table with order table using the sql join query and the foreign key relationship between the customer and order table. Then select the name and order_no data from the join table. So you are not writing two different sql queries to get data from two different table you are doing with one sql join query.

No comments :

Post a Comment