UPDATED BY
Matthew Urwin | Sep 26, 2023

Object-relational mapping (ORM) is a design pattern that helps streamline communication between relational databases and object-oriented programming languages like Java or Python

Why Use an ORM (Object-Relational Map)?

Databases and object-oriented programs use different programming languages and store data differently, which can lead to communication difficulties between languages. ORM maps objects onto the relational database table so that when we save an object to the database, it’s broken down into smaller parts that the database can store. These parts are then saved in a logical order. When we access the object again, the program can retrieve the parts from the database to reconstruct the object.

 

How Does Object-Relational Mapping Work?

ORM produces a structured map that reveals the relationships between objects and tables, or data, without getting into the minutiae of the data structure. Objects are essentially converted into digestible parts that a database can store for future retrieval. ORM connects object-oriented programming languages or applications with a relational database, communicating any changes made to an object to the database, which then alters the data accordingly.   

Because ORM monitors and handles interface changes, developers don’t need to worry about editing code on a constant basis. This technique also enables teams to understand how a database is structured without needing to review the code, making it easier to glean insights from complex states and codes generated by object-oriented programs.  

 

Object-Relational Mapping Example

Imagine a dog model (i.e. class). Each dog object has the following attributes: name, age and breed. Here is how the dog class and dog objects look in Java.

Object-relational mapping image of code written in Java defining a dog by its name, age and parameter.
A Java class that defines a dog by its name, age and parameter. | Image: Artem Oppermann

To store all of these dog objects in the database, we create a table to represent the dog class. The table rows correspond to dog instances and the columns correspond to the dog class’ attributes. When we create a dog record in the table, it’s automatically assigned an ID value.

object-relational mapping of the table described above.
The table stores the attributes of the different dog classes. | Image: Artem Oppermann

More From Artem OppermannWhat Are Data Silos?

 

ORM vs. SQL

Object-relational mapping and structured query language (SQL) are two very different techniques used for understanding and managing databases. 

The main difference between the two is that ORM uses a much higher level of abstraction than SQL to model the structure of a database. As a result, ORM involves less coding while SQL is more hands-on. However, this same distinction also makes ORM more complex. SQL allows developers to work directly with their code while ORM creates a layer between the developer and the code, so it’s difficult to see what’s happening with the code itself.

ORM provides a convenient alternative to SQL, but deciding which one to use often depends on one’s coding experience and the nature of the data-related task that needs to be completed.     

 

Advantages of Object-Relational Mapping 

Handles the Logic Required to Interact With Databases

ORM tools automatically generate the necessary SQL queries and handle database transactions so developers don’t need to write SQL code themselves. This process reduces the chance of errors and makes the code easier to maintain because the ORM tool handles the low-level details of database interactions.

 

Speeds up Development Time for Teams

ORM tools abstract away the low-level details of working with a database, which allows developers to focus on writing business logic instead of SQL queries. This shift results in faster development times because developers can write and test code more quickly.

 

Decreases the Cost of Development

Because ORM tools automate many of the repetitive tasks involved in working with databases, developers can write more code in less time, thereby leading to a lower cost of development.

 

Improves Security

ORM tools provide built-in security features that help prevent SQL injection attacks, which are a common security vulnerability in database-driven applications. ORM tools also provide a layer of abstraction between the application code and the database, thereby making it more difficult for attackers to exploit vulnerabilities.

 

You Write Less Code 

ORM tools allow developers to interact with databases using a higher-level programming language like Java or Python instead of writing SQL queries. This makes the code easier for other team members to read; the code is also easier to maintain because developers don’t need to write as much SQL code. Furthermore, ORM tools provide a standard interface to interact with databases so developers don’t have to learn the intricacies of multiple database systems. Together, all of these elements can reduce the amount of code that developers need to write.

Introduction to Object-Relational Mapping. | Video: LaunchCode

 

Disadvantages of Object-Relational Mapping 

Steep Learning Curve

ORM tools can have a steep learning curve, especially for developers who are not familiar with object-oriented programming concepts. Developers have to understand how to map their object-oriented code to the database schema, which can require a significant amount of time and effort to learn. Additionally, some ORM tools have their own specific syntax and APIs, which developers must learn to use.

 

Struggles With Complex Queries

ORM tools abstract away the details of working with a database, which can make them less efficient than writing SQL directly. This is especially true when complex queries are involved because ORM tools generate SQL queries automatically. This process can result in less efficient queries than those written by a skilled SQL developer. Furthermore, some ORM tools may not support advanced SQL features, thereby limiting their usefulness for certain applications.

 

Slow Performance Speed

ORM tools generate SQL queries automatically, which can result in queries that are less efficient than those written by an experienced SQL developer. This lack of efficiency can result in slower performance for database-driven applications. Additionally, ORM tools often generate more database queries than necessary, which can slow down performance even further. While ORM tools can help simplify development, these tools may not be the best choice for applications that need high performance or have strict performance requirements.

Find out who's hiring.
See all Developer + Engineer jobs at top tech companies & startups
View 8178 Jobs

 

Object-Relational Mapping Tools

The list below highlights some of the most popular ORM tools available for Java and Python.

 

Java

  • Hibernate: This tool allows developers to create data persistence classes using object-oriented programming (OOP) concepts such as inheritance, polymorphism and association. Hibernate is known for its high performance and scalability.
  • Apache OpenJPA: Another Java persistence tool, Apache OpenJPA can function as a standalone persistence layer for plain old Java objects (POJOs).
  • EclipseLink: This open-source Java persistence solution supports relational, XML and database web services.
  • jOOQ: jOOQ generates Java code based on the data stored in a database and can also be used to construct type-safe SQL queries.
  • Oracle TopLink: With Oracle TopLink, developers can create high-performance applications that store persistent data in either relational or XML formats.

 

Python

  • Django: Django is an efficient tool for building web applications rapidly.
  • web2py: This open-source full-stack Python framework is designed to help developers create fast, scalable, secure and data-driven web applications.
  • SQLObject: SQLObject is an object-relational manager that enables developers to use an object interface to interact with their database.
  • SQLAlchemy: This tool provides persistence patterns specifically designed to optimize database access for high performance and efficiency.

 

Frequently Asked Questions

What is object-relational mapping?

Object-relational mapping is a technique that maps objects onto a relational database, converting the object into data that can be stored, retrieved and reconstructed when needed. Through ORM, changes made to an object are shared with the database, which then updates the data to reflect these changes.

What is the difference between object-relational mapping and SQL?

The difference between object-relational mapping and SQL is that ORM uses a much higher level of abstraction to depict how data is structured. This means ORM involves less intensive coding than SQL, but it also adds a layer of complexity since developers can’t directly interact with code when using ORM.

Expert Contributors

Built In’s expert contributor network publishes thoughtful, solutions-oriented stories written by innovative tech professionals. It is the tech industry’s definitive destination for sharing compelling, first-person accounts of problem-solving on the road to innovation.

Learn More

Great Companies Need Great People. That's Where We Come In.

Recruit With Us