CSC/ECE 517 Summer 2008/wiki3 1 th
RBP/OO Interactions
- It would be good if OO programs could interact with OO databases, but alas, relational databases have a 99% market share. This has led to many attempts to access them from OO languages. Design patterns for doing this have been developed, starting with Crossing Chasms and extending to Rails' ActiveRecord [1][2]. Here, we investigate the various approaches for marrying OO programs to relational databases, comparing them in terms of ease of programming, robustness, and efficiency.
Introduction
This article explores object-relation mapping (ORM), a programming technique that bridges object-oriented languages against relational databases, and compares them against more traditional approaches to database programming such as stored procedures and dynamic SQL. In the process, we examine basic design patterns for bridging this gap, and evaluate several popular ORM frameworks found in popular program languages in the process.
One of the primary problems that object-relational mapping (ORM) attempts to solve is that of transparent object persistence, which allows an object to outlive the process that created it. The state of an object can be stored to disk, and an object with the same state can be re-created in the future. This object data is typically internally stored in a relational database using SQL.
Unfortunately, relational databases lie at the core of any modern Enterprise application, and such tabular representation of SQL data is fundamentally different than the network of objects used in object-oriented applications. ORM allows us to interact with business objects directly in an object-oriented domain model, instead of having to work with rows and columns at the programming level. For an introduction to ORM and the surrounding issues, refer to [3].
Design Patterns
Design patterns provide the theoretical underpinnings for the object-relational tools that we use in practice today. One of the first design patterns exploring the bridge between object-oriented domains and relational domains is Crossing Chasms.
Concentrate Examples
To motivate our discussions, it is helpful to provide a practical, concentrate reference example of object-relational frameworks against traditional dynamic SQL approaches. One can think of these examples as the "Hello World" of DB interfacing.
Embedded (Dynamic) SQL
The traditional approach uses simple strings and API calls to connect to databases and return their results. In this example in PHP, the title and date of the event with an ID greater than 5 is echoed:
<?php
$link = mysql_connect('localhost', 'mysql_user',
'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
$sql = "SELECT title, date
FROM events
WHERE id > 5";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
echo $row["title"];
echo $row["date"];
}
?>
Stored Procedures
Stored procedures are mechanisms that encapsulate SQL queries and other SQL business logic within the database server itself. It has the advantage of decoupling the SQL syntax from the client application, but calls to these stored procedures themselves are still not transparent from within the client application. An example of a SQL stored procedure in Microsoft SQL Server is as follows:
CREATE PROCEDURE spCaliforniaAuthors AS SELECT * FROM authors WHERE state = 'CA' ORDER BY zip
This stored procedure is kept on the database. The application can then call the stored procedure spCaliforniaAuthors directly, without concerning themselves with the lower level implementation details.
The ORM Approach
Using object-relational mapping, the internal connection details are hidden, usually in external configuration files. The application developer need not have any knowledge of SQL programming, and can manipulate and access database objects in an object-oriented domain. In this example using Java and Hibernate, the programmer created a new event and then saves it to a database:
Session session = HibernateUtil.getSessionFactory(). getCurrentSession(); session.beginTransaction(); Event theEvent = new Event(); theEvent.setTitle(title); theEvent.setDate(date); session.save(theEvent); session. getTransaction().commit();]
Microsoft LINQ
Microsoft provides a hybrid approach to object-relational mapping, called LINQ, which interleaves database metadata with object-oriented programming to allow for language-integrated querying of relational databases.
[Table(Name="Customers")] public class Customer { [Column(IsPrimaryKey=true)] public string CustomerID; [Column] public string City; }
Unlike other ORM mechanics, which try to hide non-OO querying as much as possible, or use external library calls for direct SQL queries, LINQ to SQL provides a runtime infrastructure for managing relational data as objects without losing the ability to query through SQL-like constructs:
var q = from c in Customers where c.City == "London" select c; foreach (var cust in q) Console.WriteLine("id = {0}, City = {1}", cust.CustomerID, cust.City);
These SQL-like constructs are integrated into the language itself, without the need for external library calls, and are available to non-database data structures as well, if they support the appropriate interfaces.
Comparison
With our examples in hand, we can now discuss the advantages and disadvantages of the various approaches to marrying object-oriented programming and relational databases.
Ease of Programming
Those who come from a database background find Dynamic SQL easy to use because queries are written in a SQL language that they are already familiar with. The other advantage of dynamic SQL is that it allows the application developers to exploit the properties and function of their particular database, at the expense of reducing portability of the application.
http://www.codinghorror.com/blog/archives/000117.html
ORM frameworks have the advantage of developers to work with objects and the mapping tools that enable data persistency transparently. For most applications, this provides a natural object-oriented way to access relational databases. However, mapping more difficult relational concepts to OOP, such as many-to-one and many-to-many, can be complicated. [4]
While most ORM frameworks are easy to program from an application developer perspective, the initial configuration of such frameworks can be daunting due to the framework's generality and large number of configuration parameters. Configuration of ORM is difficult enough that there exist meta code generation tools such as XDoclet and Middlegen, which can be thought of as compilers in their own right.
Robustness
Using an ORM layer provides database independence.
Efficiency
Any good studies on this??
Impedence Mismatches
ORM has experienced criticism, including the notion ORM is The Vietnam of Computer Science. There is so much here I don't know what to incorporate - please suggest.
Other problems that ORM attempts to solve are that of paradigm mismatches. These are outlined as follows: [5]
- Problems relating to subtypes. Object-oriented languages implement inheritance through superclasses and subclasses. SQL tables, in contrast, do not generally implement any sort of table inheritance, and they additionally lack an obvious way to implement polymorphism. Mapping class inheritance from the object domain to the relational domain comprises one of the many goals of ORM.
- Problems relating to associations. In domain models, associations represent the relationship between entities. Object-oriented languages represent associations using object references, but in relational databases, an association is represented through foreign keys. Object-relational mapping bridges these two concepts.
- Problems relating to data navigation. There is also a key difference in the way data is accessed in object-oriented languages and in relational databases. In OOP, one walks the object network, navigating from one object to another. This is not an efficient way to retrieve data from a SQL database, where the goal is to minimize the number of SQL queries. Efficient access in SQL relies on set operations, like joining multiple tables of interest. This mismatch between the way objects are accessed in OOP versus a relational database is the single most common source of performance problems.
Implementations
Languages and environments as diverse as .Net and PHP support ORM [6]. We will focus our comparison on specific implementations of ORM in Java, Ruby on Rails, Microsoft .NET, PHP, ASP Classic, and JDBC.
ORM in Java
In recent years, Java has experienced a paradigm shift from complex heavy-weight frameworks such as Enterprise Java Beans to more light-weight agile frameworks that rely instead of simple Plain Old Java Objects (POJOs). This in turn, has increased the popularity of ORM for Java developers.
Indeed, Object-relational mapping is especially popular in the Java community, compared. for example to .NET developers. [7] Although a plethora of ORM frameworks exist for Java, among the most popular and widespread ORM layers today include Sun's JDO and the somewhat entrenched open source O/R mapping framework, Hibernate.
We begin with Java Data Objects (JDO), which by itself is not a framework, but a specification. The API is a standard interface-based Java model abstraction of persistence, developed under the auspices of the Java Community Process. Frameworks like Apache JDO then implement this specification. JDO aims to provide implementations for not only relational databases, but also object databases, and file systems.
Hibernate is another ORM implementation, and though it is open source, it is often considered "proprietary" because it does not directly implement the JDO specification or Java Community Process specifications. Still, Hibernate's momentum has resulted in it becoming a de facto standard in the Java industry, furthered by frameworks such as Spring that use it as a building block.
http://www.kuro5hin.org/story/2006/3/11/1001/81803
ORM in Ruby on Rails
http://wiki.rubyonrails.org/rails/pages/ActiveRecord
ORM in Microsoft .NET
LINQ in Microsoft .NET
http://msdn.microsoft.com/en-us/netframework/aa904594.aspx
Dynamic SQL in PHP, ASP Classic, and JDBC
Despite the availability of object-relational libraries, dynamic SQL continues to be a popular development mechanism for interfacing with databases. Dynamic SQL is not an object-oriented methodology, but rather a "bare metal" programming approach where SQL strings are directly constructed through concatenation or other low-level mechanisms and then directly passed to the database. The results of such queries are themselves lower level objects like record sets or hash tables.
Summary
Links
http://www.google.com/search?hl=en&q=ORM&btnG=Google+Search
http://en.wikipedia.org/wiki/Object-relational_mapping
http://en.wikipedia.org/wiki/Object-relational_database
http://en.wikipedia.org/wiki/List_of_object-relational_database_management_systems
http://www.aspfree.com/c/a/Database/Introduction-to-RDBMS-OODBMS-and-ORDBMS/
http://developers.slashdot.org/article.pl?sid=03/09/23/2016224&threshold=4&mode=nested
http://en.wikipedia.org/wiki/Object-relational_impedance_mismatch
http://www.google.com/search?hl=en&q=RDB+OO+patterns+faq&btnG=Search
http://ootips.org/persistent-objects.html
http://dtemplatelib.sourceforge.net/
http://trac.butterfat.net/public/StactiveRecord
http://www.metro-design-dev.com/modeler_portal.htm
http://www.ksc.com/articles/patternlanguage.htm
http://en.wikipedia.org/wiki/ActiveRecord_%28Rails%29
http://www.agiledata.org/essays/mappingObjects.html
http://www.hibernate.org/hib_docs/reference/en/html/index.html
http://www.hibernate.org/hib_docs/v3/api/index.html
http://www.service-architecture.com/object-relational-mapping/articles/transparent_persistence.html
http://www.service-architecture.com/object-oriented-databases/articles/odbms_faq.html
http://portal.acm.org/citation.cfm?id=253810
http://www.pearsonhighered.com/educator/academic/course/0,3119,604655,00.html
http://www.cmcrossroads.com/bradapp/links/oo-links.html
http://www.edcomp.com/results/Relational+and+Object+oriented+Database+Management+System+.html
http://cbbrowne.com/info/rdbms.html
http://en.wikipedia.org/wiki/Object-oriented_programming
http://www.sei.cmu.edu/str/descriptions/oodatabase_body.html
http://www.service-architecture.com/object-oriented-databases/
http://www.arrakis.es/~devis/oo.html
http://www.google.com/search?hl=en&q=object+oriented+database+site%3Anist.gov&btnG=Google+Search
http://csrc.nist.gov/nissc/1996/papers/NISSC96/paper072_073_074/SCO_.PDF
http://madgeek.com/Articles/ORMapping/EN/mapping.htm