Definition

object-relational mapping (ORM)

What is object-relational mapping (ORM)?

Object-relational mapping (ORM) is a way to align programming code with database structures. ORM uses metadata descriptors to create a layer between the programming language and a relational database. It thus connects object-oriented program (OOP) code with the database and simplifies the interaction between relational databases and OOP languages.

The idea of ORM is based on abstraction. The ORM mechanism makes it possible to address, access and manipulate objects without having to consider how those objects relate to their data sources. ORM lets programmers maintain a consistent view of objects over time, even as the sources that deliver them, the sinks that receive them and the applications that access them change.

Developers can also perform various data creating, reading, updating and deleting (CRUD) operations in relational databases without using SQL. This capability is particularly useful for developers who either don't know SQL or don't want to waste time writing SQL code. With ORM, they don't have to understand and write SQL or rely on SQL query builders to add an abstraction layer to the SQL code.

How object-relational mapping works

ORM translates information about states and codes object-oriented programs create that are often difficult to understand. It creates a structured map that explains how objects are related to different tables (data) without knowing how the data is structured. In other words, it creates a logical model of the program with a high level of abstraction, i.e., without specifying the underlying code details.

This information helps developers understand the underlying database structure. When the application makes changes to the data object, the relational database will insert, update, create or delete data in response to these changes. This happens because the ORM converts data between tables and generates the SQL code needed by the database to respond to application changes and to manage data activities.

ORM manages the mapping details between a set of objects and underlying relational databases, XML repositories or other data sources and sinks. It simultaneously hides the often changing details of related interfaces from developers and the code they create. In many cases, ORM changes can incorporate new technology and capabilities without requiring changes to the code for related applications.

Advantages and benefits of object-relational mapping

ORM hides and encapsulates changes in the data source, so that when data sources or their APIs change, only ORM needs to change to preserve the appropriate associations -- not the applications that use ORM. This capacity makes it easier to maintain applications, lets developers take advantage of new classes as they become available and makes it easy to extend ORM-based applications.

The program model can be used to connect the application with the SQL code without rewriting the code, saving a lot of time for developers. Because the ORM will manage the application's data needs, developers don't have to write any additional low-level code, which can help to increase productivity and speed up development time.

ORM also makes it easier and more cost-effective to maintain the application over time because it automates object-to-table and table-to-object conversion and requires less code compared to embedded SQL and handwritten stored procedures. In many cases, it can improve the application's overall design. Finally, ORM improves system performance by enabling transparent object caching in the application tier.

Disadvantages of object-relational mapping

Illustration showing object-oriented programming
Object-relational mapping (ORM) connects object-oriented program (OOP) code with a database and simplifies relational database and OOP language interactions.

Critics have said that ORM can lead to an erosion in application speed and performance due to the extra code that is generated for abstraction. They believe that using stored procedures is a better way to avoid this problem. In some cases, depending on ORM might result in poorly designed databases and make it harder to maintain applications.

Incorrect mapping between data tables and objects can also create application problems that may be difficult to recognize and can affect overall performance. Finally, if the ORM layer is poorly written, it can become difficult to improve data schemas and to manage database migrations.

ORM vs. SQL

Native querying with SQL has both pros and cons. SQL queries are more flexible and detailed than ORM abstractions. SQL also enables programmers to better control their data interface. On the other hand, they are also responsible for maintaining the security of the database code, which can be difficult to do. If there are vulnerabilities in the code, the application may be at risk of SQL injection attacks. The likelihood of such attacks can be lower with ORM, because ORM tools help to "sanitize" and secure the code.

SQL query builders add a layer of abstraction over the raw SQL without masking the underlying details and simplify application integration. They include template builders so developers can understand the database structure. However, they still need to know SQL to understand the database structure.

Popular object-relational mapping tools

An ORM tool is software designed to help OOP developers interact with relational databases in a simpler manner. These tools use one of two strategies:

  • Active record pattern. The tool maps data within the structure of objects in the programming code and manages the data using classes and structures within the code.
  • Data mapper pattern. This type of tool separates the business logic (in the objects) from the underlying database, allowing reuse of the same programming logic while simplifying database changes.

The best ORM tools create high-quality code without affecting application performance, support and database migrations, and provide options to improve data schemas. They also speed up development, decrease development costs and improve application security to minimize the chances of SQL injections and other types of attacks.

A variety of ORM tools are available for different OOP languages, including PHP, Java and Python.

Tool name OOP language Key features/USPs

Laravel

PHP

  • Includes an object-relational manager. (Eloquent) to simplify database interactions
  • Pairs with MySQL, Postgres, SQLite and SQL Server.

CakePHP

PHP

  • RedisEngine supports fast deletes with deleteAsync().
  • Supports two object types: repositories -- for access to a collection of data -- and entities -- which represent individual data records.

RedBeanPHP

PHP

  • Automatically generates the database, tables and columns on the fly.
  • Uses recursive table expressions to deal with database tree structures.
  • Supports all free, open source databases.

Django

Python

  • Suitable for building web applications quickly.
  • Can interact with application data from many relational databases, including SQLite, PostgreSQL and MySQL.

Web2py

Python

  • Provides an open source full-stack Python framework for building web applications.
  • Provides a Database Abstraction Layer (DAL) to map Python objects into database objects (queries, tables, records).

SQLObject

Python

  • Provides an object interface to a database.
  • Includes a Python-object-based query language that provides substantial database independence for applications.

SQLAlchemy

Python

  • Provides persistence patterns to speed up database access.
  • Declarative Mapping structure defines both a Python object model and database metadata that describes real SQL tables in a database.

Hibernate

Java

  • Follows OOP concepts like inheritance, polymorphism, association and composition to create data persistent classes.
  • Can be used in any environment supporting Java Persistence API (JPA), including Java SE applications, Java EE application servers and Enterprise OSGi containers.

Apache OpenJPA

Java

  • Suitable as a standalone POJO (plain old Java object) persistence layer.
  • Includes the persistence and ORM schemas from JPA.

EclipseLink

Java

  • Suitable for relational, XML and database web services.
  • Provides a complete JPA-compliant implementation.

JOOQ (object oriented querying)

Java

  • Generates Java code from data stored in the database.
  • Uses a Java compiler to compile SQL syntax, metadata and data types.

Entity Framework

.NET

  • Provides a multidatabase object-database mapper.
  • Supports LINQ (language integrated query), change tracking, updates and schema migrations.

NHibernate

.NET

  • Provides multiple plugins and tools to speed up development.
  • Built on top of ADO.NET data access technology with a mature and active codebase.

Dapper

.NET

  • Supports Micro-ORM, which is useful for mapping queries to objects.
  • Provides an ORM library, which is suitable for .NET and .NET Core applications.
  • It is lightweight and fast-- ideal for applications requiring low latency and high performance.

Base One Foundation Component Library (BFC)

.NET

  • Suitable for creating networked database applications with Visual Studio and database software.
  • Works with Windows and Unix/Linux.
  • Supports distributed computing, batch processing, queuing and database command scripting.

See a breakdown of object-oriented programming concepts.

This was last updated in March 2023

Continue Reading About object-relational mapping (ORM)

Dig Deeper on Core Java APIs and programming techniques

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close