ELI5: Object-Relational Mapping

Candace Codes
3 min readJul 12, 2020

What is ORM and what is it used for?
ORM is the abbreviation for object-relational mapping and is a technique to communicate between two typically incompatible forms of data manipulation and storage. There are multiple models for how we can store data in databases, and using a relational database is one option. With ORM we can store, retrieve, update, and delete data more easily in a relational database within an OOP (object-oriented program).

ORM systems utilize what’s often referred to as a data layer which exists as a liaison for your model classes and database by managing the translation of data formats between these two worlds. (The data layer is typically a library written in the object-oriented language that works in conjunction with the web-framework that you’re using to write the rest of your application.)

Figure 1. Object-oriented data model vs relational data model

The relational database will store data in tables using rows and columns, with the columns holding our parameters of interest, like name and description. Each column has a specific data type (like int, string, or varchar) associated with it, and data is stored in the table as rows. Along with this, each table holds a primary key which allows us to easily find our object of interest (and note: each table holds its own primary key and foreign keys of interest).

Using CRUD: Create (and Save), Read, Update, Delete
Rather than manually translating our data from an object-oriented class to a relational database using a database management system like SQL, a tedious and easily complicated project, a data layer acts as the middle-man and automates that process for us.

We can send a method like “save” to the data layer, and the data layer will know what we’re requesting and will insert a new row, using a SQL command, and put in the data information into its appropriate column in the relational database.

If we’d like to retrieve data from the database, it’s often framed as a “select” statement. For instance, if we want to select a certain row from the data table, the data layer will create a new object and populate the given properties for us.

In a relational way, we create a new table with the necessary column values, primary key, which is a unique ID for our objects to simplify finding our objects, and a descriptor. The primary key is going to be what allows us to relate in a relational world in the same way we did in the object-oriented world using references.

If we want to store this relationship between objects and category objects, we can add a category column to ourtable. This will allow us to store the unique ID associated with that referenced in the object-oriented world within the table and so the database will be able to get them out, the data layer will be able to do that.

The category ID number that refers to a primary key in another table is called a foreign key. The data layer will be able to properly reference these IDs for us when we want to get them out of the database. When our data layer gets data out of the database, join queries allow us to reference multiple tables together and reconstitute our data in an object-oriented format.

References:
“Know The Difference-Learn How Object Oriented Database Is Different from RDBMS.” ODBMS Facts, 16 Jan. 2017, www.odbmsfacts.com/articles/object-oriented_databases/.

Michael , Bächle, and Paul Kirchberg. “Ruby on Rails.” Ruby on Rails — IEEE Journals & Magazine, 2007, ieeexplore.ieee.org/abstract/document/4375251?casa_token=nrsDKckGr7MAAAAA%3AqWs1X0VyatZy-xKyqz169OfCB7vwpVWS04BO1eGw7TP7vmGmsawacw5Bb8elO7kDEfyyahmNCw.

“Object-Oriented Programming.” Wikipedia, Wikimedia Foundation, 6 July 2020, en.wikipedia.org/wiki/Object-oriented_programming.

--

--