Object-Relational Mapping: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
 
(171 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''Object-relational Mapping''' (ORM, O/RM, and O/R 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.Object code is written in object-oriented programming (OOP) languages such as Java or C#. ORM converts data between type systems that are unable to coexist within relational databases and OOP languages.<ref name=techopedia>http://www.techopedia.com/definition/24200/object-relational-mapping--orm</ref>
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational Mapping (ORM, O/RM, and O/R 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.Object code is written in [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming (OOP)] languages such as [http://en.wikipedia.org/wiki/Java_(programming_language) Java], [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby], [http://en.wikipedia.org/wiki/C%2B%2B C++] or [http://en.wikipedia.org/wiki/C_Sharp_(programming_language) C#]. ORM converts data between type systems that are unable to coexist within relational databases and OOP languages.<ref name=techopedia>[http://www.techopedia.com/definition/24200/object-relational-mapping--orm Techopedia,Object-Relational Mapping]</ref>


==Overview ==
==Overview ==
In object-oriented programming, data management tasks act on object-oriented (OO) 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 (it can be referenced by a single variable containing a pointer to the object, 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.
In [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming], data management tasks act on Object-oriented (OO) 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 ( It can be referenced by a single variable containing a pointer to the object, 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 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.[1]
However, many popular database products such as [http://en.wikipedia.org/wiki/SQL Structured Query Language(SQL)] [http://en.wikipedia.org/wiki/Database Database Management Systems (DBMS)] 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 (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 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.<ref>abc</ref>
The core 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.


==ORM Architecture and Framework==
==ORM Architecture and Framework==
===Why to use ORM framework?===
ORM framework were designed to reduce the amount of work needed to develop an object-oriented system that stores data in a relational database. The structure of in-memory objects does not match up well with the structure of data in relational databases, and it can be a slow and repetitive process to develop all the code that is required to move the information between the object space and the relational space. <ref name=Frameworks>[http://http://www.adobe.com/inspire-archive/october2008/articles/article2/index.html?trackingid=DWZST,An introduction to ColdFusion frameworks]</ref>
[[File:ORM Framework.jpg|frame|center|ORM Architecture]]
===What goal to use ORM framework?===
The goal of a ORM framework is to automatically map objects to and from a database seamlessly so that information can be quickly and easily retrieved in object format, and then subsequently stored in its relational structure. This aids in system development and maintenance greatly. The mundane details of writing your own create, read, update, and delete functions are taken care of for you, so you can concentrate on designing and building your application.
Consider a project in which the application configuration is working smoothly. Your Model, View, and Controller logic is separated wonderfully, and your dependency injection framework is handling your CFC relationships. The annoying thing is, you’re spending three quarters of your time writing boring SQL statements to put data in the database, take it out, and edit it. Writing that SQL and matching it with CFCs that represent the data makes developing your application really slow, and maintaining it requires you to make numerous changes just for something simple, such as adding a column to a table.
If you use a ORM framework, things are far easier. You don’t have to write the SQL to do create, read, update, and delete operations anymore — it is generated for you. Pulling information from the database is as simple as requesting a CFC that represents it from the framework. To update that data, you simply pass that CFC back to the framework, which manages it for you. Suddenly you find that you are spending far more time working on how your application is meant to function and how to design it, rather than the mundane details of pushing and pulling data from your database.
The below figure illustrates what occurs in the mapping layer. The yellow lines show the mapping. Note, however, that the mapping lines do not connect directly to the color swatch objects. This represents that the join involving the "Size Color" table is creating the reference from the size objects to the color swatch objects.<ref>[http://www.service-architecture.com/articles/object-relational-mapping/mapping_layer.html Service Architecture-Mapping Layer]</ref>
[[File:Mapping.jpg |frame|center|Mapping Layer]]
===What does an ORM framework do?===
ORM framework generates objects that virtually map the tables in a database. As a programmer, you would use these objects to interact with the database. So the main idea, is to try and shield the programmer from having to write optimized SQL code – the ORM generated objects take care of that for you. So let’s take a look at a simple example:
Say for instance you had a database with two tables:
*Clients
*Products
With a little bit of configuration on your part, the ORM framework would create corresponding objects (say, clients_object and products_object) that would handle all the database interaction. So let’s say you need to add a new client to the database, you would just have to use the ORM’s clients_object to add the new client.
For example, it could be as simple as calling the object’s ‘save()’ method:
client = new clients_object("Stefan","Mischook");
client.save();
The above of code, is just pseudo code, mainly because the syntax will vary from ORM framework and from language to language. But hopefully you get the general idea of how much easier an ORM framework can make things.<ref name=framework>What are ORM Frameworks?, http://www.killerphp.com/articles/what-are-orm-frameworks/#sthash.xY8Xrpj9.dpuf</ref>
In other words, instead of something like this:
String sql = "SELECT ... FROM persons WHERE id = 10"
DbCommand cmd = new DbCommand(connection, sql);
Result res = cmd.Execute();
String name = res[0]["FIRST_NAME"];
===When to use an ORM framework?===
From my personal experience, an ORM framework becomes more useful as the size and complexity of the project increases. If you just have a simple database with say 5 tables and 5-6 queries … setting up an ORM framework may be overkill. I would start considering the use of ORM when:<ref name=framework/>
*You have 3 or more programmers on a web application.
*Your database consist of 10+ tables.
*You have say 10+ queries to make.
===How does Object-Relational Mapping work?===
<ref name=serviceArch>[http://www.service-architecture.com/articles/object-relational-mapping/transparent_persistence.html Service Architecture,Transparent Persistence in Object-Relational Mapping]</ref>
In object-relational mapping products, the ability to directly manipulate data stored in a relational database using an object programming language is called transparent persistence. This is in contrast to a database sub-language used by embedded SQL or a call interface used by ODBC or JDBC. See [http://www.service-architecture.com/articles/object-relational-mapping/transparent_persistence_vs_jdbc_call-level_interface.html transparent persistence vs. JDBC call-level interface.]
Using an object-relational mapping product means you will have less code to write and, depending on how you use your data, you might have higher performance over an embedded SQL or a call interface. See [http://www.service-architecture.com/articles/object-relational-mapping/caching_for_object-relational_mapping.html caching for object-relational mapping.]
With transparent persistence, the manipulation and traversal of persistent objects is performed directly by the object programming language in the same manner as in-memory, non-persistent objects. This is achieved through the use of intelligent caching as this animation shows. For coding examples, see [http://www.service-architecture.com/articles/object-relational-mapping/how_to_access_data_in_a_relational_database.html how to access data in a relational database.]
[[File:Or-mapping-transparency.gif|frame|center]]


==Comparison with Traditional Methods==
==Comparison with Traditional Methods==
Line 16: Line 67:


==Simple Example with ORM==
==Simple Example with ORM==
<ref name=sample>[http://stackoverflow.com/questions/1152299/what-is-an-object-relational-mapping-framework StackOverFlow,What is an Object-Relational Mapping Framework?]</ref>
String sql = "SELECT ... FROM persons WHERE id = 10"
DbCommand cmd = new DbCommand(connection, sql);
Result res = cmd.Execute();
String name = res[0]["FIRST_NAME"];
you do something like this:
Person p = repository.GetPerson(10);
String name = p.FirstName;
or similar code (lots of variations here.) Some frameworks also put a lot of the code in as static methods on the classes themselves, which means you could do something like this instead:
Person p = Person.Get(10);
Some also implement complex query systems, so you could do this:
Person p = Person.Get(Person.Properties.Id == 10);


==ORM Language==
See Detailed Sample in Java concerning Objected Relational Mapping at [http://www.javaworld.com/article/2077674/java-web-development/a-standardized-object-relational-mapping-mechanism-for-the-java-platform.html A standardized object-relational mapping mechanism for the Java platform]


==ORM Language and Main Tools==
This table just includes several well-known object-relational mapping tools regarding to some popular programming languages. It is not up-to-date or all-inclusive. <ref name=listOfORM>[http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software Wikipedia,List of Object-relational mapping Software]</ref>
{| class="wikitable" style="font-size: 90%;"
{| class="wikitable" style="font-size: 90%;"
|+ style="font-size: 1.25em;" |
|+ style="font-size: 1.25em;" |
Line 24: Line 95:
!style="background:#eFeFeF;"| Name
!style="background:#eFeFeF;"| Name
!style="background:#eFeFeF;"| C++
!style="background:#eFeFeF;"| C++
!style="background:#eFeFeF;"| C#
!style="background:#eFeFeF;"| .NET
!style="background:#eFeFeF;"| Objective-C
!style="background:#eFeFeF;"| Objective-C
!style="background:#eFeFeF;"| Java
!style="background:#eFeFeF;"| Java
!style="background:#eFeFeF;"| Python
!style="background:#eFeFeF;"| Python
!style="background:#eFeFeF;"| Perl
!style="background:#eFeFeF;"| Perl
!style="background:#eFeFeF;"| PHP
!style="background:#eFeFeF;"| Ruby
!style="background:#eFeFeF;"| Ruby
|-
|-
|rowspan="3"| Several Object-Relational Mapping Tools
|rowspan="3"| Several Object-Relational Mapping Tools
| LiteSQL
| LiteSQL
| [[BSD licenses|BSD]]
| ADO.NET Entity Framework
| no
| core Data
| {{yes}}
| jOOQ
|
| Django
| {{yes}}
|rowspan="3"| DBIx::Class
| {{yes}}
| CakePHP
 
| ActiveRecord
|-
|-
| ODB
| ODB
|
| Nhibernate
| [[GNU General Public License|GPL]]/other
|rowspan="2"| Enterprise Objects
| no
| Apache_Gora
| {{yes}}
| Peewee ORM
| {{yes}}
| Doctrine
| {{yes}}
|align="center" | Sequel




|-
|-
| Wt::Dbo
| Wt::Dbo
|
| OpenAccess ORM
| [[GNU General Public License|GPL]] / commercial
| Hibernate ORM
| no
| SQLAlchemy
| {{no}}
| FuelPHP
| {{yes}}
| DataMapper
| {{yes}}
 
 
|}
|}


==ORM Tools==


=== Comparison between ORM tools ===
=== Comparison between ORM tools ===
The following table lists several well-known object-relational mapping tools
<ref name=comparisonOfORM> [http://en.wikipedia.org/wiki/Comparison_of_object-relational_mapping_software Wikipedia, Comparison of Object-relational mapping Software]</ref>
{| class="wikitable sortable" style="font-size: 90%;"
|+ style="font-size: 1.25em;" |
|-
!style="background:#eFeFeF;"| ORM Tools
!style="background:#eFeFeF;"| Platform
!style="background:#eFeFeF;"| Availablity
!style="background:#eFeFeF;" class="unsortable"| Version
|-
|LiteSQL
|C++
|Open Source
|0.3.15 / August 10, 2012
|-
|ODB
|C++
| Dual-licensed(GNU GPL/Proprietary License)
| 2.3.0 / October 30, 2013
|-
|Wt::Dbo
|C++
|Dual-licensed(GNU GPL/Proprietary License)
|3.3.3 / May 27, 2014
|-
|ADO.NET Entity Framework
| .NET3.5(SP1)+
| Part of .NET 4.0
| EF5.0
|-
|Nhibernate
| .NET2.0(SP1)+
| Open Source
| NH3.3.1
|-
|OpenAccess ORM
| .NET2.0+
| Free & Commercial
| Version 2012.2.816.1
|-
| core Data
| Objective-C
| Commercial
| 3.2.0
|-
| Enterprise Objects Framework
| Objective-C
| Commercial
| WebObjects 5, released in 2001
|-
| jOOQ
| Java Virtual Machine
|Dual-licensed(ASL 2.0/Commercial)
|3.3.0 / February 14, 2014
|-
| Apache_Gora
| Java Virtual Machine
|Open Source
|0.4 / April 14, 2014
|-
|Hibernate ORM
|Java Virtual Machine
|Open Source
|4.2.5 / August 28, 2013
|-
|Django
|Python
|Open Source
|1.7 / September 2, 2014
|-
|Peewee ORM
|Python
|Open Source
|2.2.3 / April 20, 2014
|-
|SQLAlchemy
|Python
|Open Source
|0.9.4 / March 28, 2014
|-
|DBIx::Class
|Perl
|Open Source
|0.08270 / January 31, 2014
|-
|CakePHP
|PHP
|Open Source
|2.5.4 /September 2, 2014
|-
|Doctrine
|PHP
|Open Source
|2.4/April 8, 2014
|-
|FuelPHP
| PHP
| Open Source
| 1.7.2/ July 13, 2014
|-
|ActiveRecord
| Ruby
| Open Source
| 4.2.0.beta1/ Aug 20, 2014
|-
|Sequel
| Ruby
| Open Source
|4.14.0 / September 1, 2014
|-
|DataMapper
| Ruby
| Open Source
|1.2.0/October 13, 2011
|}


==ORM Advantage and Disadvantage==
==ORM Advantage and Disadvantage==
Line 84: Line 266:
*ORM does not perform well and that stored procedures might be a better solution.
*ORM does not perform well and that stored procedures might be a better solution.
*ORM dependence may result in poorly-designed databases in certain circumstances.<ref name=techopedia/>
*ORM dependence may result in poorly-designed databases in certain circumstances.<ref name=techopedia/>
*ORM is slower than hand-crafted persistence code that directly interacts with a database API such as JDBC or ADO.NET.
*Most ORM systems have unsatisfactory support for revisions to the architecture of a system after a system has been mapped.
*ORM has yet to see widespread adoption in everyday industrial operations.<ref name=abc>Barnes, Jeffrey M. Object-Relational Mapping as a Persistence Mechanism for Object-Oriented Applications. Diss. Macalester College, 2007.WebCast at http://digitalcommons.macalester.edu/mathcs_honors/6/</ref>


==Database Support==
==See Also==


==See Also==
*[http://en.wikipedia.org/wiki/Object-relational_mapping Wikipedia, Object-relational Mapping]
*[http://en.wikipedia.org/wiki/Object-oriented_programming Wikipedia, Object-oriented Programming]
*[http://hibernate.org/orm/what-is-an-orm Hibernate Overview,What is Object/Relational Mapping?]
*[http://www.tutorialspoint.com/hibernate/orm_overview.htm Object-relational Mapping Overview]
*[http://stackoverflow.com/questions/398134/what-are-the-advantages-of-using-an-orm?rq=1 StackOverFlow,Disadvantage for ORM]
*[http://www.service-architecture.com/articles/object-relational-mapping/index.html Service Architecture, Object-Relational Mapping Articles]
*[http://tech.lalitbhatt.net/2014/07/object-relationship-mapping-orm.html Object-Relational Mapping Concepts]
*[http://madgeek.com/Articles/ORMapping/EN/mapping.htm Choosing an object-relational mapping tool] by Fabrice Marguerie
*[http://www.agiledata.org/essays/mappingObjects.html#sthash.9WWl7FXB.adVJqKGo.dpuf Mapping Objects to Relational Databases: O/R Mapping In Detail]
*[http://guides.rubyonrails.org/active_record_basics.html RailsGuides Active Record Basics]
*[http://www.rubyonrails.org/ Ruby on Rails official website]
*[http://docs.oracle.com/cd/E13189_01/kodo/docs316/ref_guide_mapping.html#ref_guide_mapping_mappingtool Object-Relational Mapping]
*[https://cayenne.apache.org/docs/3.1/tutorial/ch03.html#mapping-db-relationships Getting started with Object Relational Mapping (ORM)]
*[http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/architecture.html#using-an-object-relational-mapper Architecture]
*[http://www.agiledata.org/essays/mappingObjects.html#sthash.9WWl7FXB.adVJqKGo.dpuf Mapping Objects to Relational Databases: O/R Mapping In Detail]
*[http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/orm.html Object Relational Mapping (ORM) Data Access]


==References==
==References==
<references></references>
<references></references>
==External Links==

Latest revision as of 02:42, 14 September 2014

Object-relational Mapping (ORM, O/RM, and O/R 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.Object code is written in object-oriented programming (OOP) languages such as Java, Ruby, C++ or C#. ORM converts data between type systems that are unable to coexist within relational databases and OOP languages.<ref name=techopedia>Techopedia,Object-Relational Mapping</ref>

Overview

In Object-oriented programming, data management tasks act on Object-oriented (OO) 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 ( It can be referenced by a single variable containing a pointer to the object, 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(SQL) Database Management Systems (DBMS) 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 (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 core 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.

ORM Architecture and Framework

Why to use ORM framework?

ORM framework were designed to reduce the amount of work needed to develop an object-oriented system that stores data in a relational database. The structure of in-memory objects does not match up well with the structure of data in relational databases, and it can be a slow and repetitive process to develop all the code that is required to move the information between the object space and the relational space. <ref name=Frameworks>introduction to ColdFusion frameworks</ref>

ORM Architecture

What goal to use ORM framework?

The goal of a ORM framework is to automatically map objects to and from a database seamlessly so that information can be quickly and easily retrieved in object format, and then subsequently stored in its relational structure. This aids in system development and maintenance greatly. The mundane details of writing your own create, read, update, and delete functions are taken care of for you, so you can concentrate on designing and building your application.

Consider a project in which the application configuration is working smoothly. Your Model, View, and Controller logic is separated wonderfully, and your dependency injection framework is handling your CFC relationships. The annoying thing is, you’re spending three quarters of your time writing boring SQL statements to put data in the database, take it out, and edit it. Writing that SQL and matching it with CFCs that represent the data makes developing your application really slow, and maintaining it requires you to make numerous changes just for something simple, such as adding a column to a table.

If you use a ORM framework, things are far easier. You don’t have to write the SQL to do create, read, update, and delete operations anymore — it is generated for you. Pulling information from the database is as simple as requesting a CFC that represents it from the framework. To update that data, you simply pass that CFC back to the framework, which manages it for you. Suddenly you find that you are spending far more time working on how your application is meant to function and how to design it, rather than the mundane details of pushing and pulling data from your database.

The below figure illustrates what occurs in the mapping layer. The yellow lines show the mapping. Note, however, that the mapping lines do not connect directly to the color swatch objects. This represents that the join involving the "Size Color" table is creating the reference from the size objects to the color swatch objects.<ref>Service Architecture-Mapping Layer</ref>

Mapping Layer

What does an ORM framework do?

ORM framework generates objects that virtually map the tables in a database. As a programmer, you would use these objects to interact with the database. So the main idea, is to try and shield the programmer from having to write optimized SQL code – the ORM generated objects take care of that for you. So let’s take a look at a simple example:

Say for instance you had a database with two tables:

  • Clients
  • Products

With a little bit of configuration on your part, the ORM framework would create corresponding objects (say, clients_object and products_object) that would handle all the database interaction. So let’s say you need to add a new client to the database, you would just have to use the ORM’s clients_object to add the new client. For example, it could be as simple as calling the object’s ‘save()’ method:

client = new clients_object("Stefan","Mischook");
client.save();

The above of code, is just pseudo code, mainly because the syntax will vary from ORM framework and from language to language. But hopefully you get the general idea of how much easier an ORM framework can make things.<ref name=framework>What are ORM Frameworks?, http://www.killerphp.com/articles/what-are-orm-frameworks/#sthash.xY8Xrpj9.dpuf</ref>

In other words, instead of something like this:

String sql = "SELECT ... FROM persons WHERE id = 10"
DbCommand cmd = new DbCommand(connection, sql);
Result res = cmd.Execute(); 
String name = res[0]["FIRST_NAME"];

When to use an ORM framework?

From my personal experience, an ORM framework becomes more useful as the size and complexity of the project increases. If you just have a simple database with say 5 tables and 5-6 queries … setting up an ORM framework may be overkill. I would start considering the use of ORM when:<ref name=framework/>

  • You have 3 or more programmers on a web application.
  • Your database consist of 10+ tables.
  • You have say 10+ queries to make.

How does Object-Relational Mapping work?

<ref name=serviceArch>Service Architecture,Transparent Persistence in Object-Relational Mapping</ref> In object-relational mapping products, the ability to directly manipulate data stored in a relational database using an object programming language is called transparent persistence. This is in contrast to a database sub-language used by embedded SQL or a call interface used by ODBC or JDBC. See transparent persistence vs. JDBC call-level interface.

Using an object-relational mapping product means you will have less code to write and, depending on how you use your data, you might have higher performance over an embedded SQL or a call interface. See caching for object-relational mapping.

With transparent persistence, the manipulation and traversal of persistent objects is performed directly by the object programming language in the same manner as in-memory, non-persistent objects. This is achieved through the use of intelligent caching as this animation shows. For coding examples, see how to access data in a relational database.

Comparison with Traditional Methods

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.<ref>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.</ref>

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.<ref>Josh Berkus, "Wrecking Your Database", Computer, Aug. 2009, http://it.toolbox.com/blogs/database-soup/wrecking-your-database-33298, Webcast at http://www.youtube.com/watch?v=uFLRc6y_O3s</ref>

Simple Example with ORM

<ref name=sample>StackOverFlow,What is an Object-Relational Mapping Framework?</ref>

String sql = "SELECT ... FROM persons WHERE id = 10"
DbCommand cmd = new DbCommand(connection, sql);
Result res = cmd.Execute();
String name = res[0]["FIRST_NAME"];

you do something like this:

Person p = repository.GetPerson(10);
String name = p.FirstName;

or similar code (lots of variations here.) Some frameworks also put a lot of the code in as static methods on the classes themselves, which means you could do something like this instead:

Person p = Person.Get(10);

Some also implement complex query systems, so you could do this:

Person p = Person.Get(Person.Properties.Id == 10);

See Detailed Sample in Java concerning Objected Relational Mapping at A standardized object-relational mapping mechanism for the Java platform

ORM Language and Main Tools

This table just includes several well-known object-relational mapping tools regarding to some popular programming languages. It is not up-to-date or all-inclusive. <ref name=listOfORM>Wikipedia,List of Object-relational mapping Software</ref>

Name C++ .NET Objective-C Java Python Perl PHP Ruby
Several Object-Relational Mapping Tools LiteSQL ADO.NET Entity Framework core Data jOOQ Django DBIx::Class CakePHP ActiveRecord
ODB Nhibernate Enterprise Objects Apache_Gora Peewee ORM Doctrine Sequel


Wt::Dbo OpenAccess ORM Hibernate ORM SQLAlchemy FuelPHP DataMapper


Comparison between ORM tools

The following table lists several well-known object-relational mapping tools <ref name=comparisonOfORM> Wikipedia, Comparison of Object-relational mapping Software</ref>

ORM Tools Platform Availablity Version
LiteSQL C++ Open Source 0.3.15 / August 10, 2012
ODB C++ Dual-licensed(GNU GPL/Proprietary License) 2.3.0 / October 30, 2013
Wt::Dbo C++ Dual-licensed(GNU GPL/Proprietary License) 3.3.3 / May 27, 2014
ADO.NET Entity Framework .NET3.5(SP1)+ Part of .NET 4.0 EF5.0
Nhibernate .NET2.0(SP1)+ Open Source NH3.3.1
OpenAccess ORM .NET2.0+ Free & Commercial Version 2012.2.816.1
core Data Objective-C Commercial 3.2.0
Enterprise Objects Framework Objective-C Commercial WebObjects 5, released in 2001
jOOQ Java Virtual Machine Dual-licensed(ASL 2.0/Commercial) 3.3.0 / February 14, 2014
Apache_Gora Java Virtual Machine Open Source 0.4 / April 14, 2014
Hibernate ORM Java Virtual Machine Open Source 4.2.5 / August 28, 2013
Django Python Open Source 1.7 / September 2, 2014
Peewee ORM Python Open Source 2.2.3 / April 20, 2014
SQLAlchemy Python Open Source 0.9.4 / March 28, 2014
DBIx::Class Perl Open Source 0.08270 / January 31, 2014
CakePHP PHP Open Source 2.5.4 /September 2, 2014
Doctrine PHP Open Source 2.4/April 8, 2014
FuelPHP PHP Open Source 1.7.2/ July 13, 2014
ActiveRecord Ruby Open Source 4.2.0.beta1/ Aug 20, 2014
Sequel Ruby Open Source 4.14.0 / September 1, 2014
DataMapper Ruby Open Source 1.2.0/October 13, 2011

ORM Advantage and Disadvantage

Advantage

ORM is a rapidly growing and popular methodology that provides clear advantages to the developer: Eliminates the fragility of coding CRUD statements to persist data to and from the database Allows logic, business rules and validation to be introduced to the data Provides domain objects that are easy to customise and manage in code Hides the differences between various databases or data sources Saves vast amounts of coding effort

  • Simplified development because it automates object-to-table and table-to-object conversion, resulting in lower development and maintenance costs
  • Less code compared to embedded SQL and handwritten stored procedures
  • Transparent object caching in the application tier, improving system performance
  • An optimized solution making an application faster and easier to maintain<ref name=techopedia/>

Disadvantage

  • ORM does not perform well and that stored procedures might be a better solution.
  • ORM dependence may result in poorly-designed databases in certain circumstances.<ref name=techopedia/>
  • ORM is slower than hand-crafted persistence code that directly interacts with a database API such as JDBC or ADO.NET.
  • Most ORM systems have unsatisfactory support for revisions to the architecture of a system after a system has been mapped.
  • ORM has yet to see widespread adoption in everyday industrial operations.<ref name=abc>Barnes, Jeffrey M. Object-Relational Mapping as a Persistence Mechanism for Object-Oriented Applications. Diss. Macalester College, 2007.WebCast at http://digitalcommons.macalester.edu/mathcs_honors/6/</ref>

See Also

References

<references></references>