Not to be confused with Object-Role Modeling.
Object-relational mapping in computer science is a programming technique for converting
data between incompatible type systems in object-oriented programming languages.
This creates, in effect, a "virtual object database" that can be used from within the
programming language.
There are both free and commercial packages available that perform object-relational mapping,
although some programmers opt to create their own ORM tools.
Overview In object-oriented programming, data management
tasks act on object-oriented objects that are almost always non-scalar values.
For example, consider an address book entry that represents a single person along with
zero or more phone numbers and zero or more addresses.
This could be modeled in an object-oriented implementation by a "Person object" with attributes/fields
to hold each data item that the entry comprises: the person's name, a list of phone numbers,
and a list of addresses.
The list of phone numbers would itself contain "PhoneNumber objects" and so on.
The address book entry is treated as a single object by the programming language.
Various methods can be associated with the object, such as a method to return the preferred
phone number, the home address, and so on.
However, many popular database products such as structured query language database management
systems can only store and manipulate scalar values such as integers and strings organized
within tables.
The programmer must either convert the object values into groups of simpler values for storage
in the database, or only use simple scalar values within the program.
Object-relational mapping is used to implement the first approach.
The heart of the problem is translating the logical representation of the objects into
an atomized form that is capable of being stored in the database, while preserving the
properties of the objects and their relationships so that they can be reloaded as objects when
needed.
If this storage and retrieval functionality is implemented, the objects are said to be
persistent.
Comparison with traditional data access techniques Compared to traditional techniques of exchange
between an object-oriented language and a relational database, ORM often reduces the
amount of code that needs to be written.
Disadvantages of O/R mapping tools generally stem from the high level of abstraction obscuring
what is actually happening in the implementation code.
Also, heavy reliance on ORM software has been cited as a major factor in producing poorly
designed databases.
NoSQL databases Another approach is to use an object-oriented
database management system or document-oriented databases such as native XML databases that
provide more flexibility in data modeling.
OODBMSs are databases designed specifically for working with object-oriented values.
Using an OODBMS eliminates the need for converting data to and from its SQL form, as the data
is stored in its original object representation and relationships are directly represented,
rather than requiring join tables/operations.
Document oriented databases also prevent the user from having to "shred" objects into table
rows.
Many of these systems also support the XQuery query language to retrieve datasets.
Object-oriented databases tend to be used in complex, niche applications.
One of the arguments against using an OODBMS is that switching from an SQL DBMS to a purely
object-oriented DBMS means that you may lose the capability to create application independent
queries for retrieving ad hoc combinations of data without restriction to access path.
For this reason, many programmers find themselves more at home with an object-SQL mapping system,
even though most object-oriented databases are able to process SQL queries to a limited
extent.
Other OODBMS provide replication to SQL databases, as a means of addressing the need for ad hoc
queries, while preserving well-known query patterns.
Challenges There are a variety of difficulties that arise
when considering how to match an object system to a relational database.
These difficulties are referred to as the object-relational impedance mismatch.
An alternative to implementing ORM is use of the native procedural languages provided
with every major database on the market.
These can be called from the client using SQL statements.
The Data Access Object design pattern is used to abstract these statements and offer a lightweight
object-oriented interface to the rest of the application.
See also List of object-relational mapping software
Comparison of object-relational mapping software AutoFetch - automatic query tuning
CORBA Object database
Object persistence Object-relational database
Object-relational impedance mismatch Relational model
Java Data Objects Service Data Objects
Entity Framework Active record pattern
References
External links About ORM by Anders Hejlsberg
Mapping Objects to Relational Databases: O/R Mapping In Detail by Scott W. Ambler
Core J2EE Design Pattern: Data Access Objects Choosing an Object-Relational mapping tool
Object-Relational Mapping Concepts