ORM: What is it all about? Part 1

Suryasis Paul
LaymanTech
Published in
2 min readApr 27, 2020

--

Object-relational Mapping is the mapping between objects and relational tables. Yes! That’s all there is to it. But like most things in life, that mapping is easier said than done. And here is why.

Object-relational impedance mismatch is a hefty and loaded term to explain that objects and relational tables struggle to find a symbiotic relationship. You may ask why is that so and I would give you few good thoughts to ponder over a cup of coffee:

  • Objects are more granular than relational tables. What does that mean? Well, if you have built anything more than a toy application, you would know that the number of classes in any application far exceeds the number of tables in it’s concerned database. What that means is in a typical scenario, objects hold lesser information than the tables in a database. Hence, the word granularity. Objects or tables both at their base are but units of data. And if one form of data is more granular than the other, it’s needless to say that one-on-one mapping is difficult.
  • Objects may have subtypes while a completely standardized SQL database rarely supports the concept of inheritance. So when you transfer data from an object-oriented domain to a relational domain. How do you maintain this relationship information of inheritance?
  • The question of identity. In the relational world, two objects can only be equal if they have the same primary key. For example, in the table below, both usernames with Id 1 and 2 are treated as different, even though the value contained is essentially the same.

So identity and equality mean the same thing. In the object-oriented world languages typically take a more nuanced approach to define equality and identity. For example in the example below both objects may be considered equal though they are not identical because they contain the same value.

  • Navigating the data. Navigating the data in the relational world could mean hopping from one table to another. For the same data object-oriented world, you would just need to traverse the network of objects.

YADA YADA YADA, you might say there are a ton of problems when trying to map objects into relational tables. But why is there such a problem? Is it even worth the pain to incorporate an ORM solution into a project? And what disadvantages would you face if you were to do so? All this and much more in the coming posts.

--

--