What is Object Relational Mapping?

What is Object Relational Mapping?

This article assumes you have a basic understanding of what an objected-oriented language is and SQL

If you're working with developing web applications and primarily work in backend with object-oriented languages such as Java, Ruby, C#, etc. involving databases, you would have definitely had the need to write to SQL queries to interact with the database.

What if I said we need not write a query to interact with the database?

What is ORM?

Object-relational mapping (ORM, O/RM, and O/R mapping) in computer software is a programming technique for converting data between incompatible type systems in object-oriented programming languages. This creates, in effect, a “virtual object database” that can be used from within the programming language. There are both free and commercial packages available that perform object-relational mapping, although some programmers opt to create their own ORM tools.

— Wikipedia

Object Relational Mapping is a technique of storing, retrieving, updating, and deleting from an object-oriented structure in a relational database. ORM helps developers use the OOPS language they are working on to interact with the database, instead of writing SQL queries.

Each programming language as its own Object Relational Mapper that maps the objects to the tables in a database. These objects can later be used to interact with the database.

For example,

  • Django has it's own Django ORM
  • C# has Dapper
  • Java has Hibernate
  • Rails comes with Active Record

How ORM code looks like?

The code varies for each ORM framework, depending upon the language the framework works upon.

Let's assume how a typical SQL query would like for adding a user to the users table which has the following schema.

column_namedatatype
idint
namestring
citystring
INSERT INTO users VALUES ( 1, "John Doe", "New York");

What if I say could write the same SQL query as this

User.create!(name: "John Doe", city: "New York")

The above line is how Active Record, the default Ruby on Rails ORM( Object Relational Mapper) creates a row in the users table.

Similarly, let's see another comparison for retrieving values. The below table shows how Active Record retrieves data from the table vs native SQL

ORMSQL
user_1 = User.where(name: "John Doe")SELECT * FROM users WHERE name = "John Doe";

Here, user_1 is an Object of the User class. You can modify the properties of the object and save it to see the modification reflected in the database as well.

user_1 = User.where(name: "John Doe")
user_1.name = "Michael Hartl"
user_1.save!

From the above example, we can see that the object maps to the record in the table of the database, and the properties of the class are indeed the columns of the table.

So, how does ORM add advantage to your application?

  • It maps the business model and the database
  • It's database-independent - You need not write queries specific to the database you're using.
  • Helps in the consistency of data types between the OO language and the SQL database.
  • Reduces the lines of code and increases efficiency by allowing developers to focus on business logic.
  • It shields your applications from SQL Injection attacks since frameworks filter the data for you
  • Provides a query interface to interact with the database

Drawbacks of using an ORM for your application

  • There is a learning curve involved with most ORM frameworks.
  • Actions like bulk insert, update, delete are quite slower while compared to implementing them with native SQL.
  • Though there's no need for SQL while using an ORM, developers result in not learning database internals and SQL unless some are keen on learning them.

I hope you got a better idea of what Object Relational Mapping is, and how it eases working with relational databases. Let me know if you liked learning about ORM by reacting to the article and through your comments below :)


References :

Did you find this article valuable?

Support Sabarish Rajamohan by becoming a sponsor. Any amount is appreciated!