Impedance Mismatch

Amisha Purswani
Last Updated: May 13, 2022

Introduction

Languages such as Java represent data as interconnected graphs of objects, whereas relational database management systems represent data in a table format. Java implements the object model, and relational database management systems implement the relational model because both the models are pretty different in representing data. When we load or store graphs of objects using the relational database, we come across five main mismatch problems. This mismatch between the object and relational models is called object-relational impedance mismatch or Paradigm mismatch. In simple words, it is just another way of saying that the object model and relational model do not work very well together. Loading and storing graphs of objects using a tabular relational database exposes us to 5 mismatch problems.

Granularity Mismatch

The first mismatch we come across is a granularity mismatch. Granularity is the extent to which a system could be broken down into small parts or pieces. In an application, we have many granular objects with various levels of granularity.

Example-

In an E-commerce application, a person is a screened object because it contains a lot of details about  the Person, such as 

  • Person’s address 
  • Order placed by person
  • Billing details etc.


Things like that so the Person object could be broken down into many small pieces. Another tender object in an e-commerce application is address which is a fine-grained object because it contains address-specific information such as street names, landmarks, and cities. Assume, however, that there is just one table called Person in the database for storing Person details. There are situations when an object model has more classes than the number of related tables in the database (we say the object model is more granular than the relational model). This is due to a mismatch in the granularity of the Object Model and the Relational Model.

Subtypes (inheritance) Mismatch

The second mismatch we come across is a subtype mismatch.

We have the concept of subtype or inheritance in the object model, but we do not have any such thing in the relational model. 

In the example below-

Here a book is a subtype of product in the object model, but no such thing could be defined in the relational model.

Identity Mismatch

The third Mismatch we come across is identify mismatch.

The primary key is the only notion of sameness defined by an RDBMS. Object-oriented language, e.g., Java, however, defines both object identity a==b and object equality a.equals(b). This is an identity mismatch between Object Model and Relational Model. In a relational database, the sameness of two entities is defined only by their primary keys. They are considered identical if they have the same value for their primary keys. But Java defines both object identity and object equality. Object identity is being compared in the first one here, which results in false because the IDs of the instance foo refers to are not the same as the one bar is referring to, so they are not the same. The value ID here reflects the object's address in memory. Object equality is being checked in the second one here, which will result in either true or false based on how the equal method is implemented in the class instances of age foo and bar are referring to.

Association Mismatch

The fourth Mismatch we come across is association mismatch.

Object references achieve association in the object model, whereas in the relational model, we use a foreign key to associate two entities, and object references are directional. If an association between objects should be navigable in both directions, you must define the association twice, once in each of the associated classes, just like the example below. The foreign key associations are not directional.


Associations are represented as unidirectional references in Object-Oriented languages, whereas RDBMSs use the notion of foreign keys. If you need bidirectional relationships in Java, you must define the association twice. Likewise, you cannot determine the diversity of a relationship by looking at the object domain model.

Data navigation Mismatch

The way you access data in Java differs significantly from how you do it in a relational database. In Java, you navigate the object network from one association to another. This method of getting data from a relational database is inefficient. You usually wish to reduce the number of SQL queries by loading numerous entities via JOINs and selecting the appropriate entities before walking the object network.

The solution for the above problems is to use the 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 a 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 RowsColumns, and Tables facilitated by Object/Relational Mapper.

FAQs

  1. What is impedance mismatch?
    Impedance mismatch refers to issues that arise due to mismatches between the database and programming language models. 
     
  2. What is granularity?
    Granularity refers to the degree to which a system can be broken down into tiny pieces. 
     
  3. How can be the problems of impedance mismatch can be solved?
    The database's role is to serve as a persistence layer. These issues arise when attempting to convert an object model's entity into a database model's entity. As a result, it is the job of an application programmer to handle these issues. This is where an ORM can help us to handle these situations.

Key Takeaways

In this blog, we have learned about impedance mismatch through some examples. Impedance mismatch focuses on problems arising from mismatches between the database and programming language models. In simple words, it is just another way of saying that the object model and relational model do not work very well together. Also, we learned about how five problems cause impedance mismatch and how Object-Relational Mapping (ORM) helps us to overcome these issues.  

Visit here to learn more about different topics related to database management systems. Also, try CodeStudio to practice programming problems for your complete interview preparation. Ninja, don't stop here; check out the Top 100 SQL Problems to get hands-on experience with frequently asked interview questions and land your dream job.

Was this article helpful ?
1 upvote

Comments

No comments yet

Be the first to share what you think