Skip to main content

What is Object-Relational Mapping (ORM)?

Object-relational mapping (ORM) is the process of abstracting the connection between programming language entities (objects) and their corresponding database elements.

When building applications, programmers define objects in code, which correspond to real-world concepts. Object data is manipulated in code but stored in a database as defined by the data system design. For example, you may have a customer object containing customer information in your code, but the underlying database could store that data across multiple tables.

Object-relational mapping is a software layer that translates object data to the underlying database, abstracting database details from the programmer. That way, the code remains unchanged even if the underlying databases are swapped out with new or different designs. Object-relational mapping allows for the creation of modular applications that are easy to manage and maintain.

How do object-relational mappings work?

Object-relational mappings (ORM) connect applications written in an object-oriented programming (OOP) language to an underlying database. These types of applications and databases are often used together, but each handles data differently. Adding an object-relational mapping between them allows each to send data in a format that the other understands. You can modify one without impacting the other.

Object-oriented programming language

OOP languages organize data into objects, which are code structures with one or more attributes. For example, in an OOP application, you could define an object represented as a data-persistent class, such as a car, with attributes like make, model, VIN, color, and mileage.

Some of the most popular object-oriented programming (OOP) languages include Python, Java, JavaScript, and .NET/C#.

Relational databases

Relational databases structure data into tables, similar to a series of connected spreadsheets. Each table consists of a set of columns with unique records in each row.

For example, your car database could have several tables. One could contain brand data, such as make, model, and VIN. Another table could store attributes of individual cars, like owner, color, and mileage. Some of the most popular relational databases include MySQL, PostgreSQL, and SQL Server.

In web development, an Object-Relational Mapping (ORM) tool converts data sent from an object-oriented application to a relational database without requiring the application to know how that data is structured in the database.

Object-relational mappings

You can create an object-relational mapping, sometimes referred to as an object-relational manager, that instructs your database on where to locate all the data requested by an application, even though each structure stores its data in a different manner. When your application requests all attributes of an individual car, the ORM instructs the database how to retrieve all of that data across multiple tables.

Because the mappings are abstracted, if the database structure ever changes or you migrate to a new database, the ORM can still point to the correct data with minimal updates.

Common ORM frameworks

ORM tools, or frameworks, are available for data modeling in many of the most popular object-oriented programming languages. Some of the most popular include:

  • Hibernate for Java
  • SQLAcademy for Python
  • Entity Framework for .NET/C#

Can you use an ORM with a non-relational database?

No, non-relational databases store data in different formats and interact with applications in different ways. The most common type of non-relational database is a document database. Documents can also have many attributes, similar to objects, but they have different programmatic structures.

Some of the most popular non-relational databases include MongoDB and NoSQL.

What is the difference between ORM and ORDBMS?

An Object-Relational Database Management System (ORDBMS) is a type of database that uses elements of both relational and object-oriented frameworks. It can store records structured as objects similar to what you can code in an OOP language. But you can also perform SQL-like relational queries on those records. That combination of features allows you to store a wider range of data types than are possible in a standard relational database.

Where an object-relational mapping (ORM) is a data abstraction that operates at the application level, an ORDBMS provides object-oriented capabilities directly at the database level.

ORMs run in your application code and generate SQL behind the scenes. ORDBMS manages data storage and query execution at the database level, with native support for advanced object types.

What is the difference between ORM and SQL?

SQL (Structured Query Language) is the standard language used to interact with relational databases. It allows you to create and manage database schemas, insert and update data, and query records using declarative commands.

SQL gives direct, low-level control over the database. ORM provides a higher-level, more abstract interface, often making development faster and code more maintainable.

ORM is ideal for simplifying common database tasks and aligning with application code, but SQL remains essential when you need performance tuning, complex queries, or full control over data access.

Why use ORM instead of SQL queries?

SQL requires you to write commands manually to interact with the database, whereas an ORM automatically translates your object-oriented code into SQL behind the scenes.

ORMs help reduce the amount of coding needed to develop an object-oriented program that must make many database queries. Instead of manually programming numerous database queries that will need to be maintained, your developers program how to send requests to the ORM.

Working in a more understandable programming language also makes code easier to read and debug.

What are the benefits of using object-relational mapping?

ORM systems bring the following benefits.

Faster application development

ORMs help retrieve complex data using understandable code in the OOP language in which your developers are most familiar. They can handle routine database operations, such as creating, reading, updating, and deleting records (CRUD), so your developers don't have to code these operations repeatedly within your application.

Simpler application maintenance

By abstracting database queries through an ORM, developers can work in a single language, making their code easier to understand and maintain.

Improved security

A SQL injection attack is a malicious attempt to access private data by sending SQL queries through user forms on the application. ORMs use parameterized queries, which first send your database a query with placeholder values, not real data. (e.g., ? or :param). The real data is sent separately. This ensures that user input is treated as data, not executable SQL code. Using an ORM tool thus prevents unauthorized data access via SQL injection.

Improved performance

An ORM can improve system performance by enabling transparent object caching in the application tier. It can save the most commonly-retrieved data closer to the application for faster access.

What are the challenges of using object-relational mappings?

ORMs present the following challenges.

Impedance mismatches

Complex data structures can be difficult to map between object-oriented programs and databases. Hierarchies that are possible to code in OOP can be very challenging to represent in a database, regardless of how sophisticated your ORM is.

Performance issues at higher complexity

Directly querying a database is more computationally efficient than using an ORM. For highly complex queries, it may be more efficient to use SQL code rather than an Object-Relational Mapping (ORM) tool.

Learning curve

While ORMs help you avoid needing to learn complex SQL code, your developers will still need to learn how to use ORM tools.

When should you use object-relational mappings?

Since an ORM tool abstracts and automates database queries, your developers can focus more of their effort on the application rather than coding potentially complex queries. Consider using ORM when:

Your application has many objects

Manually building queries for complex object interfaces and structures can be a time-consuming and effort-intensive task. Allowing an ORM tool to map objects to data stored in your database can make accurate retrieval faster and more reliable.

Your application repeats similar database queries

ORMs can easily automate many routine CRUD operations (Create, Read, Update, and Delete data), so you have less SQL code needed.

Schema evolution is ongoing

If your data model is still evolving, an ORM can help manage database schema changes more easily. Many ORMs include migration tools that let you version and apply changes in a structured way, and reduce the risk of inconsistency across environments.

Database portability matters

An ORM is an abstraction layer that purposefully separates operations invoked by your application from any one specific database. While not a guarantee of full portability, this abstraction reduces the effort involved in moving between systems. So, for example, if your car application needs to switch from using a MySQL database to PostgreSQL, you won't need to rewrite the entire application. You just need to make minor adjustments to your ORM if any of the tables change.

When should you not use object-relational mappings?

While they are powerful tools, there are still circumstances where object-relational mappers are not the best solution.

For simple database interactions

If you are building an application that only performs straightforward CRUD operations, the time and effort required to maintain an ORM may be greater than simply maintaining the queries manually within your application.

When high-performance computing is critical

For many applications, the additional computational time and resources added by an ORM are irrelevant. But for tasks like real-time analytics, where milliseconds matter, that may not be acceptable.

When you need to use denormalized schemas

In some situations, your developers may want to maintain redundant data across multiple tables in your database web services, referred to as denormalized schemas. That can improve performance when an application needs to make complex relational queries that join many tables. Mapping these queries in an ORM is not always effective.

How can AWS help with your data management requirements?

AWS provides a range of cloud database solutions that make data management efficient and cost-effective.

Amazon Relational Database Service (Amazon RDS) is a managed service that enables easy setup, operation, and scaling of a relational database in the cloud. It provides cost-efficient and resizable capacity, while managing time-consuming database administration tasks, freeing you to focus on your applications and business.

Amazon Aurora is a modern relational database service that offers high performance and high availability at scale. It offers fully open-source MySQL and PostgreSQL-compatible editions and a range of developer tools for building serverless and machine learning (ML)-driven applications.

Amazon Redshift powers modern data analytics at scale, delivering up to three times better price-performance and seven times better throughput than other cloud data warehouses.

Amazon DocumentDB (with MongoDB compatibility) is a fast, scalable, highly available, and fully managed enterprise document database that stores data in documents similar to JSON objects. They utilize the drivers returned from native objects to the programming language used by the developer, eliminating the need for ORM tools.

Get started with databases on AWS by creating a free account today.