CSC/ECE 517 Fall 2007/wiki2 2 22

From Expertiza_Wiki
Revision as of 03:03, 22 October 2007 by Sbpatel (talk | contribs)
Jump to navigation Jump to search

Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).

Introduction

What is ORM?

Object Relational Mapping (ORM) is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent data from the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers. For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.

A comprehensive list of ORM systems by language is available at List of object-relational mapping software

How ORM works?

Typical ORM systems will map one row in a table in the database to an object in the application and vice versa. In static languages the mapping is done manually or it is automated with a preprocessor or with external mapping data (e.g. XML mapping files in Java Hibernate). In languages that support meta programming at runtime or compile time the mapping can be done automatically from information gathered from the database. ORM systems typically provide the programmer with object factories that map class method calls to SQL queries. They also provide methods to store the object data back in the database after updates in the application.

ORM Implementations

Active Record

The Active Record pattern describes the most basic mapping of one object per table row, or as described by its author Martin Fowler:

An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data. [1]

One of the best examples of the implementation of the pattern is found in the Rails framework (http://ar.rubyonrails.com/).

The major features of the Rails Active Record implementation are:

  • Automated mapping between classes and tables, attributes and columns.
  • Associations between objects controlled by simple meta-programming macros.
  • Aggregations of value objects controlled by simple meta-programming macros.
  • Validation rules that can differ for new or existing objects.
  • Acts that can make records work as lists or trees
  • Callbacks as methods or queues on the entire life cycle (instantiation, saving, destroying, validating, etc).
  • Observers for the entire life cycle
  • Inheritance hierarchies
  • Transaction support on both a database and object level.
  • Reflections on columns, associations, and aggregations
  • Direct manipulation (instead of service invocation like Java Hibernate)
  • Database abstraction through simple adapters (~100 lines) with a shared connector
  • Logging support for Log4r and Logger

Crossing Chasms pattern

Crossing Chasms is a large pattern language that as a whole addresses the issues faced by teams trying to build large client-server systems using Object Technology and Relational data stores. The authors, Kyle Brown and Bruce Whitenack, originally presented the language at the Second Pattern Languages of Program Design [PLoPD] conference, the proceedings of the conference were published as the book Pattern Languages of Program Design 2.

Currently there is little information available online. The best articles still available are:

Hibernate

Hibernate supports key OO features

  • Hibernate supports natural OO idiom; inheritance, polymorphism, composition and the Java collections framework
  • It supports fine-grained object models - a rich variety of mappings for collections and dependent objects
  • There is no extra code generation or bytecode processing steps in build procedure
  • Hibernate is extremely performant, has a dual-layer cache architecture, and may be used in a cluster
  • Hibernate addresses both sides of the problem; not only how to get objects into the database, but also how to get them out again
  • Hibernate supports both long-lived persistence contexts, detach/reattach of objects, and takes care of optimistic locking automatically
  • Hibernate implements the Java Persistence management API and object/relational mapping options (EJB 3.0), two members of the Hibernate team are active in the expert group

[2]

Oracle TopLink

Oracle TopLink delivers complete Java Persistance platform by providing following main features:

  • Access relational data using JPA and many advanced object-relational extensions
  • Efficiently manipulate XML through a Java domain model using JAXB
  • Interact with unstructured data through Service Data Objects (SDO)
  • Expose relational datbases flexibly using Web Services

[3]

JDO

The Java Data Objects (JDO) API is a standard interface-based Java model abstraction of persistence,

  • Application programmers can focus on their domain object model and leave the details of persistence (field-by-field storage of objects) to the JDO implementation.
  • Applications written with the JDO API can be run on multiple implementations without recompiling or changing source code. Metadata, which describes persistence behavior external to the Java source code including most commonly used features of O/R mapping, is highly portable.
  • Applications written with the JDO API are independent of the underlying database. JDO implementations support many different kinds of transactional data stores, including relational and object databases, XML, flat files, and others.
  • Application programmers delegate the details of persistence to the JDO implementation, which can optimize data access patterns for optimal performance.
  • Applications can take advantage of EJB features such as remote message processing, automatic distributed transaction coordination, and security, using the same domain object models throughout the enterprise.

[4]

Java Persistence API

The Java Persistence API draws upon the best ideas from persistence technologies such as Hibernate, TopLink, and JDO. Customers now no longer face the choice between incompatible non-standard persistence models for object/relational mapping. The Java Persistence API contains a full object/relational mapping specification supporting the use of Java language metadata annotations and/or XML descriptors to define the mapping between Java objects and a relational database. It supports a rich, SQL-like query language (which is a significant extension upon EJB QL) for both static and dynamic queries. It also supports the use of pluggable persistence providers. The Java Persistence API is now the preferred persistence API for use with EJB 3.0 applications. [5]

Apache ObJectRelationalBridge - OJB

  • The PersistenceBroker kernel API and all top-level APIs (ODMG, OTM, JDO) allows Java Programmers to store and retrieve Java Objects in/from (any) JDBC-compliant RDBMS
  • Transparent persistence: classes does not have to inherit from any OJB base class nor implement a special interface. OJB delivers pure transparent persistence for POJOs.
  • Scalable architecture that allows to build massively distributed and clustered systems.
  • Quality assurance taken seriously: More than 800 JUnit Test Cases for regression tests. JUnit tests are integrated into the build scripts and used as quality assurance for daily development.
  • Support for Polymorphism and Extents. You can use Interface-types and abstract classes as attribute types in your persistent classes. Queries are also aware of extents: A query against a baseclass or interface will return matches from derived classes, even if they are mapped to different DB-tables
  • Support for persistent object caching. Different caching strategies and distributed caches.
  • OJB uses an XML based Object/Relational mapping. The mapping resides in a dynamic MetaData layer, which can be manipulated at runtime through a simple Meta-Object-Protocol (MOP) to change the behaviour of the persistence kernel.
  • OJB provides several advanced O/R features like Object Caching, lazy materialization through virtual proxies and distributed lock-management with configurable Transaction-Isolation levels. Optimistic and pessimistic locking is supported.
  • OJB provides a flexible configuration and plugin mechanism that allows to select from set of predefined components or to implement your own extensions and plugins.

[6]

Enterprise Objects Framework (Apple)

Enterprise Objects Framework provides tools for defining an object model and mapping it to a data model. This allows to create objects that encapsulate both data and the methods for operating on that data, while taking advantage of the data access services provided by the Framework that make it possible for these objects to persist in a relational database. The flexible, three-tier architecture provided by the Framework allows to build robust, scalable, client/server applications. Objects at each of the three tiers (user interface, enterprise objects, and data store) can be deployed to take advantage of network resources. For example, data might be stored in a relational database running on a fault-tolerant database server with gigabytes of disk storage, while enterprise objects run on high-end compute servers. Partitioning the application to make best use of available resources allows complex applications to achieve maximum performance. The portions of the Framework can be used selectively to meet specific application requirements. For example, the components that provide users with the ability to interactively manipulate enterprise objects can be used by a non-database application to handle user interface refresh and undo. We can use a custom data store (such as a flat-file system) in place of a relational database to store data for enterprise objects. Or we can make use of the database adapters separate from the rest of the Framework components to provide direct access to relational databases for your applications. [7]

Criteria for Selection

There is no perfect single tool exists. Hence the most important thing when searching for the right tool is to define precisely which criteria are essential. Following is the summary of criteria which can be considered while selecting the ORM product:

ORM specific criteria

Basic features:

  • Be able to use inheritance, create hierarchies between entities, and use polymorphism (we are using objects!). The tools can support a variety of combinations for tables and classes to allow these mechanisms.
  • Handle any type of relations (1-1, 1-n, n-n)
  • Support for transactions
  • Aggregates (equivalent to SQL's SUM, AVG, MIN, MAX, COUNT)
  • Support for grouping (SQL's GROUP BY)
  • Supported databases. A big advantage of mapping tools is that they provide an abstraction of the underlying database engine. Most of them allow switching easily between RDBMSs

Flexibility

  • Customization of queries. HQL is a strong point of Hibernate/NHibernate. We could also wish a dynamic mapping to be possible from developer provided SQL queries.
  • Support any type of SQL joins (inner join, outer join)
  • Be able to map a single object to data coming from multiple tables (joins, views). And Be able to dispatch the data from a single table to multiple objects.

Performance

  • Lazy loading - he loading of some data is deferred until it's needed. For the data through relations and for some columns.
  • Cache dynamically generated queries and Cache some data to avoid too many calls to the data source.
  • Handle cascade updates. Deleting a master record should delete the linked details if wished so.

Evolution:

  • Maintainability - what happens if the database schema changes? If I need to add a new collection?
  • Possibility to move to a new mapping tool
  • Serialization. Serialization can be used to persist data outside of the database. Serialization can be done into a binary format, or more important, in XML (

Non ORM specific criteria

  • Price
  • Performance
  • Resource consumption (memory)
  • Scalability
  • Complexity (or simplicity...)
  • Ease of use, time to be up and running
  • Documentation quality, Support, forums, community
  • Maturity, Frequency of the updates, bug fixes, evolutions
  • Vendor's reputation and stability
  • Support for multiple platforms

References

Further reading

External links

  1. http://ar.rubyonrails.com/
  2. http://www.ksc.com/article5.htm
  3. http://www.hibernate.org
  4. http://www.oracle.com/technology/products/ias/toplink/index.html
  5. http://java.sun.com/jdo
  6. http://java.sun.com/javaee/technologies/persistence.jsp
  7. http://db.apache.org/ojb/
  8. http://developer.apple.com/
  9. http://en.wikipedia.org/wiki/Object-relational_mapping
  10. http://c2.com/cgi-bin/wiki?ObjectRelationalToolComparison