CSC/ECE 517 Fall 2014/ch1a 25 jf: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
 
(101 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==Object-relational Mapping==
==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. It creates 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 choose 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#]. All in all, ORM is used to connect object code to a relational database and 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>
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational Mapping] (ORM, O/RM, and O/R mapping) is a programming framework that allows you to define a mapping between application object model and the relational database. In an [http://en.wikipedia.org/wiki/Object_model Object model], the application objects are not aware of the database structure. Objects have properties and references to other objects. [http://en.wikipedia.org/wiki/Database Databases] consist of tables with columns that maybe related to other tables.  
 
ORM provides a bridge between the [http://en.wikipedia.org/wiki/Relational_database Relational database] and the object model and lead to a huge reduction in lines of code as ORM tools translate the query language into the appropriate syntax for the database for developers. By using ORM, you can access and update data entirely using the object model of an application. There are lots of free and commercial packages available that perform object-relational mapping. [http://en.wikipedia.org/wiki/Object_code Object code] can be written using [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#].


==Background==
==Background==
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 and the home address.
When we develop an object-oriented (OO) systems<ref>[http://en.wikipedia.org/wiki/Object-oriented_analysis_and_design Object-oriented analysis and design]</ref>, there's always a mismatch between the object model and the relational database. Database management system (DMS)<ref>[http://www.eecs.berkeley.edu/Research/Areas/DBMS/ Database Management Systems (DBMS)]</ref> represent data in a tabular format whereas object-oriented languages, such as Java, Ruby or C# represent it as an interconnected graph of objects.


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.
In practice, we usually face lots of mismatch problems while working on an OO system. For example:
*Modifing the design of our database after having developed few pages or our application;
*Loading and storing objects in a relational database exposes us to the some mismatch problems;
*Having an object model which has more classes than the number of corresponding tables in the database;
*DMS doesn't define anything similar to Inheritance which is a natural paradigm in object-oriented programming languages and etc.  


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.
By using ORM, the mismatch properties and relationships of the objects in an application can be easily stored and retrieved from a database without writing SQL statements<ref>[http://search.cpan.org/~rehsack/SQL-Statement-1.405/lib/SQL/Statement.pm SQL::Statement]</ref> directly and with less overall database access code.<ref>[http://guides.rubyonrails.org/active_record_basics.html Active Record Basics]</ref>


==ORM Architecture and Framework==
==ORM Architecture and Framework==
===Why to use ORM framework?===
===Why to use ORM framework?===
ORM framework were designed to reduce the amount of work, which needed to develop an object-oriented system that stores data in a relational database. The core problem is that the structure of in-memory objects does not match up well with the structure of data in relational databases. It is always a very slow and repetitive process to develop all the codes that is required to move the information between the object space and the relational space, but ORM framework can make these things easier.<ref name=Frameworks>[http://http://www.adobe.com/inspire-archive/october2008/articles/article2/index.html?trackingid=DWZST,An introduction to ColdFusion frameworks]</ref>
ORM framework are often used to reduce the amount of work, when developing an object-oriented system that stores data in a relational database. It is always a very slow and repetitive process to develop all the codes that is required to transfer the information between the object space and the relational space, but with the help of ORM framework, this trivial process can become much easier.
 
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 the object format, and then subsequently stored in its relational structure. This can make things easier in the process of system development and maintenance. The details of writing your own create, read, update, and delete methods are automatically generated for you, so you can concentrate on designing and building for your application.The below picture is ORM framework.<ref name=Frameworks>[http://www.service-architecture.com/articles/object-relational-mapping/mapping_layer.html, An introduction to ColdFusion frameworks]</ref>


[[File:ORM Framework.jpg|frame|center|ORM Architecture]]
[[File:ORM Framework.jpg|frame|center|ORM Architecture]]


===What goal does ORM framework want to accomplish?===
===When to use an 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 the object format, and then subsequently stored in its relational structure. This can make things easier in the process of system development and maintenance. The details of writing your own create, read, update, and delete methods are automatically generated for you, so you can concentrate on designing and building for your application.
From most of programmers' experience, an ORM framework becomes more useful as the size and complexity of the project increases.Setting up an ORM framework may be unnecessary if you only have a simple database with 5 tables and 5-6 queries. In that case,when you have the following situations, the ORM framework will probably save you lots of time:
*3 or more programmers on a web application.
*database consist of 10+ tables.
*10+ queries to make.
 
In most cases, writing SQL and matching the data make developing your application really slow, and maintaining still needs to do numerous changes just for something simple, such as adding a column to a table. If using an ORM framework<ref>[http://www.orm-designer.com/orm-frameworks ORM-frameworks]</ref>, things will be far easier. It doesn't need to write the SQL statement to do create, read, update, and delete operations anymore, because it is automatically generated for you. Pulling information from the database is as simple as sending parameters to the framework. To update the data, it just needs to pass parameters back to the framework, which will manages it. Therefore, it can save lots of time to work on how the application is meant to function and how to design it, rather than the mundane details of pushing and pulling data from your database.


Consider a project in which the application configuration is working smoothly. Your Model, View, and Controller is separated wonderfully. So it is possible that you’re spending three quarters of your time writing boring SQL statements to put data in the database, take it out, and edit it. In practice, writing SQL and matching the data make developing your application really slow, and maintaining still needs to do numerous changes just for something simple, such as adding a column to a table.
===What does an ORM framework do?===
ORM framework generates objects that virtually map the tables in a database and developers would use that to interact with the database. Rather than having to write optimized SQL code, programmers could use the object generated by the ORM framework to access the database.


In this case, if using a ORM framework, things will be far easier. It doesn't need to write the SQL statement to do create, read, update, and delete operations anymore — because it is automatically generated for you. Pulling information from the database is as simple as sending parameters to the framework. To update the data, it just needs to pass parameters back to the framework, which will manages it. Therefore, it can save lots of time to work on how the application is meant to function and how to design it, rather than the mundane details of pushing and pulling data from your database.
The following is a simple example:


The below figure illustrates what occurs via using ORM framework in a project. The yellow lines show the details of mapping process. The mapping lines do not connect directly to the color swatch objects, it means 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>
For instance you had a database with the table:
[[File:Mapping.jpg |frame|center|Mapping Layer]]
*Clients


===What does an ORM framework do?===
The ORM framework would create corresponding objects (e.g. clients_object) that would handle all the database interaction once the ORM framework is well configured. If you need to add a new client to the database,what you need to do is just use the clients_object created by the ORM.
ORM framework generates objects that virtually map the tables in a database<ref name=framework>What are ORM Frameworks?, http://www.killerphp.com/articles/what-are-orm-frameworks/#sthash.xY8Xrpj9.dpuf</ref>. 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:
For example, it could be as simple as calling the object’s ‘save()’ method:


  client = new clients_object("Stefan","Mischook");
  client = new clients_object("Stefan","Mac");
  client.save();
  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.
The above of code, is just pseudo code, mainly because the syntax will vary from ORM framework and from language to language.The below figure illustrates what occurs via using ORM framework in a project.<ref>[http://www.service-architecture.com/articles/object-relational-mapping/mapping_layer.html Service Architecture-Mapping Layer]</ref>
[[File:Mapping.jpg |frame|center|ORM framework]]
 
===How does ORM framework work?===
In most of ORM tools, the ability to directly manipulate data stored in a relational database using an object programming language is called transparent persistence. It is different from a database sub-language used by embedded SQL or a call interface used by [http://en.wikipedia.org/wiki/Open_Database_Connectivity ODBC] or [http://en.wikipedia.org/wiki/Java_Database_Connectivity 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.]
 
By using an ORM tool, it means the framework will generate lots of code for you depending on how you use your data. You will 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<ref>[http://stackoverflow.com/questions/1231295/how-does-orm-work-under-the-covers-also-what-is-the-best-way-to-have-persistent How does ORM work under the covers?]</ref>. 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 between ORM and 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. However, when it comes to the Enterprise Application Development, maybe ORM will lead to other problems.
 
===Simple Example with ORM===
The Traditional Methods should first deal with a sql command and then set up a database connection. With the correct configuration of the database such as port, username and password, we will get the result of the database command and after that we can get what we want, the '''Object'''.<ref name=sample>[http://stackoverflow.com/questions/1152299/what-is-an-object-relational-mapping-framework StackOverFlow,What is an Object-Relational Mapping Framework?]</ref>


In other words, instead of something like this:
//Traditional Method
String sql = "SELECT ... FROM jobs WHERE id = 7"
DbCommand cmd = new DbCommand(connection, sql);
Result res = cmd.Execute();
Integer title = res[0]["TITLE"];


String sql = "SELECT ... FROM persons WHERE id = 10"
The Object Relational Mapping tools have configured the database for us. As developers, what we need to do is to get the Object. Therefore, what we need to do is to deal with the Object, in this case 'Job'.
DbCommand cmd = new DbCommand(connection, sql);
Result res = cmd.Execute();
String name = res[0]["FIRST_NAME"];


===How does Object-Relational Mapping work?===
//Using ORM Mapping
<ref name=serviceArch>[http://www.service-architecture.com/articles/object-relational-mapping/transparent_persistence.html Service Architecture,Transparent Persistence in Object-Relational Mapping]</ref>
Job p = repository.GetJob(7);
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 [http://en.wikipedia.org/wiki/Open_Database_Connectivity ODBC] or [http://en.wikipedia.org/wiki/Java_Database_Connectivity 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.]
Integer title = p.Title;


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.]
The code for different languages are similar, some frameworks also put a lot of the code in as static methods on the classes themselves, which means we could implement the process of getting Object even simpler.


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.]
Job p = Job.Get(7);


[[File:Or-mapping-transparency.gif|frame|center]]
For some languages which implement complex query systems, we may use the following:
 
Job p = Job.Get(Job.Properties.Id == 7);
 
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 Advantage and Disadvantage===
====Advantage====
*ORM provides a way to facilitate implementing the [http://en.wikipedia.org/wiki/Domain_model Domain model], which means that developers can model entities based on real business concepts rather than based on database structure. ORM tools are strong of query capability and will translate the query language into the appropriate syntax for the database. This leads to huge reduction in lines of code and the ORM tools allows developers to focus on the business logic of the application rather than repetitive [http://en.wikipedia.org/wiki/Create,_read,_update_and_delete CRUD] (Create Read Update Delete) logic.
 
*By using ORM tools, developers don't need to modify through different layers of the application if they want to make changes on SQL Update, Delete and Insert statements.The ORM will automatically use the updated structure for retrievals and updates once the object definitions have been updated<ref name=framework>[http://www.killerphp.com/articles/what-are-orm-frameworks/#sthash.xY8Xrpj9.dpuf What are ORM Frameworks?]</ref>.


===When to use an ORM framework?===
*ORM allows developers to navigate object relationships transparently with related objects automatically loaded as needed. Also the configurable data loading will help to load the appropriate data for each scenario.  
From most of programmers' experience, an ORM framework becomes more useful as the size and complexity of the project increases. If you just have a simple database with 5 tables and 5-6 queries, setting up an ORM framework may be overkill. So when to start considering the use of ORM:
*You have 3 or more programmers on a web application.
*Your database consist of 10+ tables.
*You have 10+ queries to make.


*Equipped with concurrency support, ORM allows multiple users to update the same data simultaneously. Also ORM has transaction management and isolation patterns,which will scope the object changes to a transaction so that the entire transaction can either be committed or rolled back. Multiple transactions can be active in memory in the same time as well, keeping each transactions changes isolated form on another.


==Comparison with Traditional Methods==
*Due to the cache management in ORM, entities are cached in memory thereby reducing load on the database.
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>
*ORM's key management characteristics will propagate and manage identifiers and surrogate keys for developers when regarding to the database operations. <ref>[http://blogs.msdn.com/b/gblock/archive/2006/10/26/ten-advantages-of-an-orm.aspx MSDN,Ten advantages of an ORM (Object Relational Mapper)]</ref>


==Simple Example with ORM==
====Disadvantage====
<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:
ORM seems have so many advantage regarding to small and personal web design and development, however, when it comes to large enterprise systems in '''Enterprise Application Development''', it may lead to some problems because of its characteristics.<ref name=disadvantage>[http://stackoverflow.com/questions/398134/what-are-the-advantages-of-using-an-orm?rq=1 StackOverFlow,Disadvantage for ORM in Enterprise Application Development]</ref>


Person p = repository.GetPerson(10);
*ORM technologies require configuration files to map table schemas into object structures,however, such configurations in large enterprise systems grow very quickly and become extremely difficult to create and manage. Maintaining the configuration also gets tedious and unmaintainable as business requirements and models constantly change and evolve in an agile environment.
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:
*ORM framework is weak in dealing with customized query, which is also used in large enterprises. Mapping custom queries that do not fit into any defined object nor supported or recommended by the framework providers. In order to solve this problem, developers have to find work-arounds by writing ad hoc objects and queries, or writing custom code to get the data they need.


Person p = Person.Get(10);
*Some of the ORM tools require proprietary binding(proprietary libraries and proprietary object query languages) that may not be standardized in the computer science industry. If the provider offers little or no flexibility to change, the developer won't be able to make a change to the specific modification unless to collaborate with the providers which will cost more time.


Some also implement complex query systems, so you could do this:
*The [http://en.wikipedia.org/wiki/Object_Query_Language Object Query Languages] provided by ORM seem like a benefit to Object Oriented developers by saving their time writing SQL. However, it will probably become a problem to developers in real industry since these query languages may not support some of the intermediate to advanced SQL constructs when Object Query Languages perform queries on the object model.<ref>[http://www.combodo.com/IMG/pdf/OQL_Reference.pdf Object Query Language Reference]</ref>


Person p = Person.Get(Person.Properties.Id == 10);
*ORM layers are using costly operations to instantiate and populate the objects with data from the database. Because of the little flexibility in the ORM framework, performance tuning the SQL is almost impossible. Therefore, the untuned and unoptimized queries produced by Object Query will cause significant performance losses and overloading of the database management systems.


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]
*Caches in ORM provide a way to relieve the load on database by reducing the roundtrips but in the meantime it lead to synchronized problems. If not maintained and synchronized in a multi-tiered implementation, significant ramifications in terms of data-accuracy and concurrency will appear. To solve this problem,third party caches or external caches have to be plugged in,pressing more burden on the data-access layer.


==ORM Language and Main Tools==
==ORM Language and Main Tools==
Line 134: Line 160:




=== Comparison between ORM tools ===
=== Comparison between ORM tools/mechanism ===
The following table lists several well-known object-relational mapping tools  
The following table lists several well-known object-relational mapping tools/mechanism. The table clarifies the ORM tools/mechanism by their platform, availability and current versions. The listed ORM tools/mechanism almost covered most of the popular Object Oriented languages.
<ref name=comparisonOfORM> [http://en.wikipedia.org/wiki/Comparison_of_object-relational_mapping_software Wikipedia, Comparison of Object-relational mapping Software]</ref>
<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%;"
{| class="wikitable sortable" style="font-size: 90%;"
|+ style="font-size: 1.25em;" |
|+ style="font-size: 1.25em;" |
|-
|-
!style="background:#eFeFeF;"| ORM Tools
!style="background:#eFeFeF;"| ORM Tools/Mechanism
!style="background:#eFeFeF;"| Platform
!style="background:#eFeFeF;"| Platform
!style="background:#eFeFeF;"| Availablity
!style="background:#eFeFeF;"| Availablity
Line 250: Line 276:
|1.2.0/October 13, 2011  
|1.2.0/October 13, 2011  
|}
|}
==ORM Advantage and Disadvantage==
===Advantage===
*Facilitates implementing the Domain Model pattern. This one reason supercedes all others. In short using this pattern means that you model entities based on real business concepts rather than based on your database structure. ORM tools provide this functionality through mapping between the logical business model and the physical storage model.
*Huge reduction in code. ORM tools provide a host of services thereby allowing developers to focus on the business logic of the application rather than repetitive CRUD (Create Read Update Delete) logic.
*Changes to the object model are made in one place. One you update your object definitions, the ORM will automatically use the updated structure for retrievals and updates. There are no SQL Update, Delete and Insert statements strewn throughout different layers of the application that need modification.
*Rich query capability. ORM tools provide an object oriented query language. This allows application developers to focus on the object model and not to have to be concerned with the database structure or SQL semantics. The ORM tool itself will translate the query language into the appropriate syntax for the database.
*Navigation. You can navigate object relationships transparently. Related objects are automatically loaded as needed. For example if you load a PO and you want to access it's Customer, you can simply access PO.Customer and the ORM will take care of loading the data for you without any effort on your part.
*Data loads are completely configurable allowing you to load the data appropriate for each scenario. For example in one scenario you might want to load a list of POs without any of it's child / related objects, while in other scenarious you can specify to load a PO, with all it's child LineItems, etc.
*Concurrency support. Support for multiple users updating the same data simultaneously.
*Cache managment. Entities are cached in memory thereby reducing load on the database.
*Transaction management and Isolation. All object changes occur scoped to a transaction. The entire transaction can either be committed or rolled back. Multiple transactions can be active in memory in the same time, and each transactions changes are isolated form on another.
*Key Management. Identifiers and surrogate keys are automatically propogated and managed.<ref>http://blogs.msdn.com/b/gblock/archive/2006/10/26/ten-advantages-of-an-orm.aspx</ref>
===Disadvantage===
Disadvantages regarding '''Enterprise Application Development'''.<ref name=disadvantage>[http://stackoverflow.com/questions/398134/what-are-the-advantages-of-using-an-orm?rq=1 StackOverFlow,Disadvantage for ORM in Enterprise Application Development]</ref>
*Configuration: ORM technologies require configuration files to map table schemas into object structures. In large enterprise systems the configuration grows very quickly and becomes extremely difficult to create and manage. Maintaining the configuration also gets tedious and unmaintainable as business requirements and models constantly change and evolve in an agile environment.
*Custom Queries: The ability to map custom queries that do not fit into any defined object is either not supported or not recommended by the framework providers. Developers are forced to find work-arounds by writing ad hoc objects and queries, or writing custom code to get the data they need. They may have to use Stored Procedures on a regular basis for anything more complex than a simple Select.
*Proprietary binding: These frameworks require the use of proprietary libraries and proprietary object query languages that are not standardized in the computer science industry. These proprietary libraries and query languages bind the application to the specific implementation of the provider with little or no flexibility to change if required and no interoperability to collaborate with each other.
*Object Query Languages: New query languages called Object Query Languages are provided to perform queries on the object model. They automatically generate SQL queries against the database and the user is abstracted from the process. To Object Oriented developers this may seem like a benefit since they feel the problem of writing SQL is solved. The problem in practicality is that these query languages cannot support some of the intermediate to advanced SQL constructs required by most real world applications. They also prevent developers from tweaking the SQL queries if necessary.
*Performance: The ORM layers use reflection and introspection to instantiate and populate the objects with data from the database. These are costly operations in terms of processing and add to the performance degradation of the mapping operations. The Object Queries that are translated to produce unoptimized queries without the option of tuning them causing significant performance losses and overloading of the database management systems. Performance tuning the SQL is almost impossible since the frameworks provide little flexibility over controlling the SQL that gets auto generated.
*Tight coupling: This approach creates a tight dependency between model objects and database schemas. Developers don't want a one-to-one correlation between database fields and class fields. Changing the database schema has rippling affects in the object model and mapping configuration and vice versa.
*Caches: This approach also requires the use of object caches and contexts that are necessary to maintain and track the state of the object and reduce database roundtrips for the cached data. These caches if not maintained and synchronized in a multi-tiered implementation can have significant ramifications in terms of data-accuracy and concurrency. Often third party caches or external caches have to be plugged in to solve this problem, adding extensive burden to the data-access layer.


==See Also==
==See Also==


*[https://docs.google.com/document/d/1eq3XHiUUBIrEBx-R2Mgolaw7HttSgzPniwDLEF5oVk0/edit Write up]
*[http://en.wikipedia.org/wiki/Object-relational_mapping Wikipedia, Object-relational Mapping]
*[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://en.wikipedia.org/wiki/Object-oriented_programming Wikipedia, Object-oriented Programming]
*[http://en.wikipedia.org/wiki/Object_Query_Language Wikipedia, Object Query Language]
*[http://hibernate.org/orm/what-is-an-orm Hibernate Overview,What is Object/Relational Mapping?]
*[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://www.tutorialspoint.com/hibernate/orm_overview.htm Object-relational Mapping Overview]

Latest revision as of 23:03, 27 September 2014

Object-relational Mapping

Object-relational Mapping (ORM, O/RM, and O/R mapping) is a programming framework that allows you to define a mapping between application object model and the relational database. In an Object model, the application objects are not aware of the database structure. Objects have properties and references to other objects. Databases consist of tables with columns that maybe related to other tables.

ORM provides a bridge between the Relational database and the object model and lead to a huge reduction in lines of code as ORM tools translate the query language into the appropriate syntax for the database for developers. By using ORM, you can access and update data entirely using the object model of an application. There are lots of free and commercial packages available that perform object-relational mapping. Object code can be written using object-oriented programming (OOP) languages such as Java, Ruby, C++ or C#.

Background

When we develop an object-oriented (OO) systems<ref>Object-oriented analysis and design</ref>, there's always a mismatch between the object model and the relational database. Database management system (DMS)<ref>Database Management Systems (DBMS)</ref> represent data in a tabular format whereas object-oriented languages, such as Java, Ruby or C# represent it as an interconnected graph of objects.

In practice, we usually face lots of mismatch problems while working on an OO system. For example:

  • Modifing the design of our database after having developed few pages or our application;
  • Loading and storing objects in a relational database exposes us to the some mismatch problems;
  • Having an object model which has more classes than the number of corresponding tables in the database;
  • DMS doesn't define anything similar to Inheritance which is a natural paradigm in object-oriented programming languages and etc.

By using ORM, the mismatch properties and relationships of the objects in an application can be easily stored and retrieved from a database without writing SQL statements<ref>SQL::Statement</ref> directly and with less overall database access code.<ref>Active Record Basics</ref>

ORM Architecture and Framework

Why to use ORM framework?

ORM framework are often used to reduce the amount of work, when developing an object-oriented system that stores data in a relational database. It is always a very slow and repetitive process to develop all the codes that is required to transfer the information between the object space and the relational space, but with the help of ORM framework, this trivial process can become much easier.

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 the object format, and then subsequently stored in its relational structure. This can make things easier in the process of system development and maintenance. The details of writing your own create, read, update, and delete methods are automatically generated for you, so you can concentrate on designing and building for your application.The below picture is ORM framework.<ref name=Frameworks>An introduction to ColdFusion frameworks</ref>

ORM Architecture

When to use an ORM framework?

From most of programmers' experience, an ORM framework becomes more useful as the size and complexity of the project increases.Setting up an ORM framework may be unnecessary if you only have a simple database with 5 tables and 5-6 queries. In that case,when you have the following situations, the ORM framework will probably save you lots of time:

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

In most cases, writing SQL and matching the data make developing your application really slow, and maintaining still needs to do numerous changes just for something simple, such as adding a column to a table. If using an ORM framework<ref>ORM-frameworks</ref>, things will be far easier. It doesn't need to write the SQL statement to do create, read, update, and delete operations anymore, because it is automatically generated for you. Pulling information from the database is as simple as sending parameters to the framework. To update the data, it just needs to pass parameters back to the framework, which will manages it. Therefore, it can save lots of time to work on how the application is meant to function and how to design it, rather than the mundane details of pushing and pulling data from your database.

What does an ORM framework do?

ORM framework generates objects that virtually map the tables in a database and developers would use that to interact with the database. Rather than having to write optimized SQL code, programmers could use the object generated by the ORM framework to access the database.

The following is a simple example:

For instance you had a database with the table:

  • Clients

The ORM framework would create corresponding objects (e.g. clients_object) that would handle all the database interaction once the ORM framework is well configured. If you need to add a new client to the database,what you need to do is just use the clients_object created by the ORM.

For example, it could be as simple as calling the object’s ‘save()’ method:

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

The above of code, is just pseudo code, mainly because the syntax will vary from ORM framework and from language to language.The below figure illustrates what occurs via using ORM framework in a project.<ref>Service Architecture-Mapping Layer</ref>

ORM framework

How does ORM framework work?

In most of ORM tools, the ability to directly manipulate data stored in a relational database using an object programming language is called transparent persistence. It is different from 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.

By using an ORM tool, it means the framework will generate lots of code for you depending on how you use your data. You will 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<ref>How does ORM work under the covers?</ref>. For coding examples, see how to access data in a relational database.

Comparison between ORM and 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. However, when it comes to the Enterprise Application Development, maybe ORM will lead to other problems.

Simple Example with ORM

The Traditional Methods should first deal with a sql command and then set up a database connection. With the correct configuration of the database such as port, username and password, we will get the result of the database command and after that we can get what we want, the Object.<ref name=sample>StackOverFlow,What is an Object-Relational Mapping Framework?</ref>

//Traditional Method
String sql = "SELECT ... FROM jobs WHERE id = 7"
DbCommand cmd = new DbCommand(connection, sql); 
Result res = cmd.Execute();
Integer title = res[0]["TITLE"];

The Object Relational Mapping tools have configured the database for us. As developers, what we need to do is to get the Object. Therefore, what we need to do is to deal with the Object, in this case 'Job'.

//Using ORM Mapping
Job p = repository.GetJob(7);
Integer title = p.Title;

The code for different languages are similar, some frameworks also put a lot of the code in as static methods on the classes themselves, which means we could implement the process of getting Object even simpler.

Job p = Job.Get(7);

For some languages which implement complex query systems, we may use the following:

Job p = Job.Get(Job.Properties.Id == 7);

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

ORM Advantage and Disadvantage

Advantage

  • ORM provides a way to facilitate implementing the Domain model, which means that developers can model entities based on real business concepts rather than based on database structure. ORM tools are strong of query capability and will translate the query language into the appropriate syntax for the database. This leads to huge reduction in lines of code and the ORM tools allows developers to focus on the business logic of the application rather than repetitive CRUD (Create Read Update Delete) logic.
  • By using ORM tools, developers don't need to modify through different layers of the application if they want to make changes on SQL Update, Delete and Insert statements.The ORM will automatically use the updated structure for retrievals and updates once the object definitions have been updated<ref name=framework>What are ORM Frameworks?</ref>.
  • ORM allows developers to navigate object relationships transparently with related objects automatically loaded as needed. Also the configurable data loading will help to load the appropriate data for each scenario.
  • Equipped with concurrency support, ORM allows multiple users to update the same data simultaneously. Also ORM has transaction management and isolation patterns,which will scope the object changes to a transaction so that the entire transaction can either be committed or rolled back. Multiple transactions can be active in memory in the same time as well, keeping each transactions changes isolated form on another.
  • Due to the cache management in ORM, entities are cached in memory thereby reducing load on the database.

Disadvantage

ORM seems have so many advantage regarding to small and personal web design and development, however, when it comes to large enterprise systems in Enterprise Application Development, it may lead to some problems because of its characteristics.<ref name=disadvantage>StackOverFlow,Disadvantage for ORM in Enterprise Application Development</ref>

  • ORM technologies require configuration files to map table schemas into object structures,however, such configurations in large enterprise systems grow very quickly and become extremely difficult to create and manage. Maintaining the configuration also gets tedious and unmaintainable as business requirements and models constantly change and evolve in an agile environment.
  • ORM framework is weak in dealing with customized query, which is also used in large enterprises. Mapping custom queries that do not fit into any defined object nor supported or recommended by the framework providers. In order to solve this problem, developers have to find work-arounds by writing ad hoc objects and queries, or writing custom code to get the data they need.
  • Some of the ORM tools require proprietary binding(proprietary libraries and proprietary object query languages) that may not be standardized in the computer science industry. If the provider offers little or no flexibility to change, the developer won't be able to make a change to the specific modification unless to collaborate with the providers which will cost more time.
  • The Object Query Languages provided by ORM seem like a benefit to Object Oriented developers by saving their time writing SQL. However, it will probably become a problem to developers in real industry since these query languages may not support some of the intermediate to advanced SQL constructs when Object Query Languages perform queries on the object model.<ref>Object Query Language Reference</ref>
  • ORM layers are using costly operations to instantiate and populate the objects with data from the database. Because of the little flexibility in the ORM framework, performance tuning the SQL is almost impossible. Therefore, the untuned and unoptimized queries produced by Object Query will cause significant performance losses and overloading of the database management systems.
  • Caches in ORM provide a way to relieve the load on database by reducing the roundtrips but in the meantime it lead to synchronized problems. If not maintained and synchronized in a multi-tiered implementation, significant ramifications in terms of data-accuracy and concurrency will appear. To solve this problem,third party caches or external caches have to be plugged in,pressing more burden on the data-access layer.

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

The following table lists several well-known object-relational mapping tools/mechanism. The table clarifies the ORM tools/mechanism by their platform, availability and current versions. The listed ORM tools/mechanism almost covered most of the popular Object Oriented languages. <ref name=comparisonOfORM> Wikipedia, Comparison of Object-relational mapping Software</ref>

ORM Tools/Mechanism 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

See Also

References

<references></references>