CSC/ECE 517 Fall 2007/wiki2 2 aa

From Expertiza_Wiki
Revision as of 20:31, 21 October 2007 by Lzhong (talk | contribs) (→‎Hibernate)
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

Object-oriented (o-o) programming uses Object to manage data. Objects have identity, state, and behavior, which means Object is focus on identity and behavior of the things in real world. However relational database is focus on information. It is a collection of relation variables between the attributes of tables, it describes the complete state of an information model.

For instance, a "Customer Object" in on line bookstore system implemented by o-o programming constains the information of the customer such as name, address, email, and phone number. It also constains some methods operated by customer, such as "ordering_books", which defines the ordering behavior of the customer.

However, the popular relational database, such as MS SQL products, can only stores and manipulate the information of the customer within table "Customer". The ordering behavior is not included in the "Customer" table.

Therefore, programmers have to transform values between object in o-o programming and relational database. Object-ralational mapping is introduced to solve this problem.

Object-relational Mapping

Object-relational mapping (O/RM, ORM, and O/R mapping) is a programming technique or process to transform data between relational database and a set of objects in object-oriented programming.

Implementations

ActiveRecord in Ruby

Ruby ActiveRecord maps the relational database tables with objects. The main features includes:

  • 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 lifecycle (instantiation, saving, destroying, validating, etc).
  • Observers for the entire lifecycle
  • Inheritance hierarchies
  • Transaction support on both a database and object level. The latter is implemented by using Transaction::Simple
  • Reflections on columns, associations, and aggregations
  • Direct manipulation (instead of service invocation)
  • Database abstraction through simple adapters (~100 lines) with a shared connector
  • Logging support for Log4r and Logger

A detailed explanation including examples can be found here[1]

Crossing Chasms Pattern

Hibernate

Hibernate is a mature system following traditional view of O/R mapping, it also introduces innovations to streamline the mapping. Hibernate is originally developed for Java, but now it also support .NET from 2005.

Hibernate uses mapping metadata in the form of XML files, instead of specific interfaces or specicial wrapper object.

For instance, we have a class Customer[] as follows:

public class Customer { private int customerNumber; private String name; private String Address; private String telephoneNumber; /* Accessors and other methods go here. These are generally not part of the class-to-table mapping process. Remember, the database is for storing the data of the class, not its behavior. */ }

<hibernate-mapping> <class name="Customer" table="Customer" discriminator-value="P"> <id name="id" column="CustomerId" type="long"> <generator class="native"/> </id> <property name="name" column="Name" /> <property name="streetAddress" column="StreetAddress" /> <property name="postalCode" column="PostalCode" /> <property name="country" column="Country" /> <property name="telephoneNumber" column="TelephoneNumber" /> </class> </hibernate-mapping>

Service Data Object

Comparison

In Hibernate, you'd usually begin development by working on your Java objects because Hibernate is a mapping framework. The object model becomes the center of your Hibernate universe. Active Record is a wrapping framework, so you start by creating a database table. The relational schema is the center of your Active Record universe. Active Record also includes some features that many Java frameworks don't have, such as model-based validation.Model-based validation lets you make sure the data within your database stays consistent.

Reference

  1. Wikipedia Definition
  2. Foundations of Object-Relational Mapping
  3. ORM Thesis
  4. Crossing borders: Exploring Active Record

External Links

  1. O/R Mapping Products

222. [2]

333. [3]

444. [4]