Object-relational mapping
From Wikipedia, the free encyclopedia
Object-relational mapping (ORM, O/RM, and O/R mapping) in computer software is a programming technique for converting data between incompatible type systems in relational databases and 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.
Contents |
[edit] Overview
Data management tasks in object-oriented (OO) programming are typically implemented by manipulating 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 "slots" to hold the data that comprise the entry: the person's name, a list of phone numbers, and a list of addresses. The list of phone numbers would itself contain "phone number objects" and so on. The address book entry is treated as a single value by the programming language (it can be referenced by a single variable, for instance). 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 (SQL DBMS) can only store and manipulate scalar values such as integers and strings organized within normalized tables. The programmer must either convert the object values into groups of simpler values for storage in the database (and convert them back upon retrieval), 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 those objects to forms that can be stored in the database for easy retrieval, while preserving the properties of the objects and their relationships; these objects are then said to be persistent.
[edit] Pros and cons
ORM often reduces the amount of code needed to be written,[1] making the software have fewer errors.
There are cons as well as benefits for using O/R mapping. For instance, some O/R mapping tools do not perform well during bulk deletions of data. There is a myth about Stored procedures having better performance, but SQL Server Books Online says:
"SQL Server 2000 and SQL Server version 7.0 incorporate a number of changes to statement processing that extend many of the performance benefits of stored procedures to all SQL statements. SQL Server 2000 and SQL Server 7.0 do not save a partially compiled plan for stored procedures when they are created. A stored procedure is compiled at execution time, like any other Transact-SQL statement. SQL Server 2000 and SQL Server 7.0 retain execution plans for all SQL statements in the procedure cache, not just stored procedure execution plans."
[edit] Non-SQL databases
Another solution is to use an object-oriented database management system (OODBMS), which, as the name implies, is a database 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.
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 SQL queries, a tried and tested method for retrieving ad-hoc combinations of data. For this reason, many programmers find themselves more at home with an object-SQL mapping system, even though most commercial object-oriented databases are able to process SQL queries to a limited extent. Some OODBMS products support full ANSI SQL. [2]
[edit] See also
- List of object-relational mapping software
- AutoFetch - automatic query tuning
- CORBA
- Database
- Object database
- Object persistence
- Object-relational database
- Object-relational impedance mismatch
- Relational model
- SQL
- Service Data Objects
[edit] References
- ^ Douglas Barry, Torsten Stanienda, "Solving the Java Object Storage Problem," Computer, vol. 31, no. 11, pp. 33-40, Nov. 1998, http://www2.computer.org/portal/web/csdl/doi/10.1109/2.730734, Excerpt at http://www.service-architecture.com/object-relational-mapping/articles/transparent_persistence_vs_jdbc_call-level_interface.html. Lines of code using O/R are only a fraction of those needed for a call-level interface (1:4). For this exercise, 496 lines of code were needed using the ODMG Java Binding compared to 1,923 lines of code using JDBC.
- ^ Objectivity/SQL++ - an ANSI SQL interface to an OODBMS.
| This article needs additional citations for verification. Please help improve this article by adding reliable references. Unsourced material may be challenged and removed. (May 2009) |
[edit] 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
- Why use an Object-Relational mapping tool
- Perl's DBIx::Class ORM
- Object-relational mappers for Python
- A tutorial with video explaining ORM
- QuickDB