Object Relational Mapping (ORM)

What is an ORM – The Meaning of Object Relational Mapping Database Tools

Object Relational Mapping (ORM) is a technique used in creating a "bridge" between object-oriented programs and, in most cases, relational databases.

Put another way, you can see the ORM as the layer that connects object oriented programming (OOP) to relational databases.

When interacting with a database using OOP languages, you'll have to perform different operations like creating, reading, updating, and deleting (CRUD) data from a database. By design, you use SQL for performing these operations in relational databases.

While using SQL for this purpose isn't necessarily a bad idea, the ORM and ORM tools help simplify the interaction between relational databases and different OOP languages.

What is an ORM Tool?

An ORM tool is software designed to help OOP developers interact with relational databases. So instead of creating your own ORM software from scratch, you can make use of these tools.

Here's an example of SQL code that retrieves information about a particular user from a database:

"SELECT id, name, email, country, phone_number FROM users WHERE id = 20"

The code above returns information about a user — name, email, country, and phone_number — from a table called users. Using the WHERE clause, we specified that the information should be from a user with an id of 20.

On the other hand, an ORM tool can do the same query as above with simpler methods. That is:

users.GetById(20)

So the code above does the same as the SQL query. Note that every ORM tool is built differently so the methods are never the same, but the general purpose is similar.

ORM tools can generate methods like the one in the last example.

Advantages of Using ORM Tools

Here are some of the advantages of using an ORM tool:

  • It speeds up development time for teams.

  • Decreases the cost of development.

  • Handles the logic required to interact with databases.

  • Improves security. ORM tools are built to eliminate the possibility of SQL injection attacks.

  • You write less code when using ORM tools than with SQL.

Disadvantages of Using ORM Tools

  • Learning how to use ORM tools can be time consuming.

  • They are likely not going to perform better when very complex queries are involved.

  • ORMs are generally slower than using SQL.

Summary

In this article, we talked about Object Relational Mapping. This is a technique used to connect object oriented programs to relational databases.

We listed some of the popular ORM tools for different programming languages.

We concluded with some of the advantages and disadvantages of using ORM tools. languages.

Happy coding!

Last updated