CSC/ECE 517 Fall 2007/wiki2 2 22
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.
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:
- Crossing Chasms: The Architectural Patterns
- A Pattern Language for Relational Databases and Smalltalk
- Crossing Chasms: A Pattern Language for Object-RDBMS Integration "The Static Patterns"
- Crossing Chasms: A Pattern Language for Object-RDBMS Integration
- Crossing Chasms
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
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
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.
EJB CMP Entity Beans
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.