Object-Relational Mapping

From Expertiza_Wiki
Jump to navigation Jump to search

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>

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 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]

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>

ORM Architecture and Framework

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

ORM Language and Main Tools

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


Wt::Dbo OpenAccess ORM Hibernate ORM SQLAlchemy FuelPHP DataMapper

This table just includes several well-known object-relational mapping tools. It is not up-to-date or all-inclusive. <ref name=listOfORM>http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software</ref>

Comparison between ORM tools

ORM Tools Platform Version
LiteSQL ADO.NET Entity Framework core Data
ODB Nhibernate


Wt::Dbo OpenAccess ORM Hibernate ORM

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

Database Support

See Also

References

<references></references>


External Links