Computer scienceFundamentalsEssentialsSoftware constructionSoftware distribution

Object-Relational Mapping (ORM)

8 minutes read

A programming language can be used to process data from one state to another. If you want to save the state of data permanently, some kind of storage is required. You would normally use a relational database for this purpose, but there's a problem with this approach. Programming languages and databases work with data differently. Relational databases also have their own language called Structured Query Language (SQL).

Luckily, we're not the first people in the world to encounter this issue. There is a technique known as Object-Relational Mapping (ORM) that helps solve the issue. Almost every programming language includes an ORM library. So let's uncover what ORM is all about so you can start using it!

ORM concept

ORM enables you to convert data from an object-oriented programming language into the format used by a relational database and vice versa. It solves the problem of matching two different system types together and synchronizes data flow between them.

ORM converting data from objects to relational database and vice versa

The main parts of ORM are virtual tables, relationships, and operations on objects. In the following sections, you will look at what these elements are and why they are necessary.

Virtual tables

Relational databases use tuples and tables to store data. Most programming languages have tuples but don't have tables. So, how can someone represent tables in a programming language?

The main idea is to use classes as table descriptions. You can create a class as a virtual table that represents a particular table in the database. Then, use or define methods for this class that make it possible to retrieve, change, and delete data.

To represent a single row from the table, you can define another class (some libraries use the table class itself for this purpose) and match its attributes to the table's columns. An instance of this class can manipulate both the row's values and its relationships.

Let's look at an example where a class represents a City. The table's columns match the class's attributes. In this example, name is a string, while longitude and latitude are numbers.

City class as virtual table representing an actual City table in database

As you know, a class can also have attributes that represent lists of other objects. For instance, a city may have lots of streets. This format doesn't fit naturally with attribute-to-column mapping, but ORM provides ways to handle these cases too. In a relational database, the link between one row and several other rows is called a one-to-many relationship. This is similar to a class instance that has a list of objects as an attribute.

Relationships

A relationship is a link that connects a value from one table to a row in another table. The database can store such links as keys. You can think of them as objects containing another object as an attribute.

Database relationships are more than simple links, though. When you delete the root row from one table, it can cause cascade deletions of all related rows in other tables.

Deleting a row from a table in a database will remove all its elements. If your database has a table called City that contains the row London, you can expect all related rows in the table Street to disappear when the London row is deleted. A street belongs to a city; without the city, there are no streets!

a relationship connecting a value from City table to multiple rows in Street table

A programming language could either represent the above example as a class instance called City containing a list of streets or as many instances of a class called Street, each containing a City attribute. However, if you delete a street with a city value, the city will stay in the database. This is because database relationships are directional. Cascade deletions cause the removal of rows that are dependent on the table that has been deleted.

Operations on objects

The four most common database functions are known as CRUD (Create, Read, Update, Delete) operations. They are similar to those that can be carried out on objects in programming languages.

Once you're familiar with tables and the relationships between them, you can begin to control them using an ORM library. These libraries usually provide high-level commands that look like those used for working with any other objects in the programming language. Understanding how databases are structured can help you to avoid corrupting the data they contain.

Reading an ORM library's documentation before using it is highly recommended. Doing this can help you get acquainted with the effects and consequences of using its operations.

Pros and cons

If you're still unsure whether you should use ORM in your projects, considering the pros and cons will help you decide.

Pros:

  • You can work with a database in the same way that you write code for other programming languages.

  • The library frees you from understanding SQL and the specifics of the SQL dialect used by a particular database.

  • Code that uses ORM is usually easier to read, write, and maintain.

Cons:

  • Sometimes ORM libraries generate inefficient database queries.

  • It can be hard to control the generation of queries.

  • You can't use all the features and strengths of a specific database.

  • You still need to learn how to work with the library itself.

If your project needs a database, it's normally good practice to start by using ORM. Taking this approach is a sensible move because it's still possible to work directly with the database further down the line if you realize that you want greater control. In most cases though, ORM will provide everything you require.

To start working with an ORM library, you can consider SQLAlchemy for Python, Hibernate or JPA for Java/Kotlin, and Sequelize or TypeORM for JavaScript.

Conclusion

ORM makes it possible to interact with a database in an object-oriented way. Most programming languages have an ORM library that you can use for this purpose.

The main elements of ORM are virtual tables, relationships, and operations on objects. A key concept to remember is that class instances are used to represent tables and their rows.

Using an ORM library is usually the simplest way to add a database to your project. But you still need to be familiar with the library itself to do this effectively. To avoid corrupting data, it's important to understand how databases are structured before controlling them with this technique.

547 learners liked this piece of theory. 12 didn't like it. What about you?
Report a typo