In this chapter we cover an overview of mapping relational databases to object-oriented databases in order to provide object persistence.

In this chapter we examine how object-oriented can be incorporated with relational models.

Object-relational mapping

Definition of structural relationships between object-oriented and relational representations of data, typically to enable the use of a relational database to provide persistence for objects.

 

Object-relational impedance mismatch

The conceptual differences between the object-oriented approach to application design and the relational model for database design and implementation.

 

Elements of the Object-Relational Impedance Mismatch

•Nature and granularity of data types

•Structural relationships:

•Inheritance structures

•Representation of associations

•Defining the identity of objects/entity instances

•Methods of accessing persistent data

•Focus on data (relational databases) versus integrated data and behavior (the object-oriented approach)

•Architectural styles

•Support for managing transactions

 

The most typical approaches for providing persistence for objects include call-level application programming interfaces, SQL query mapping frameworks, and object-relational mapping frameworks.

 

Call-level application programming interface

A mechanism that provides an application program with access to an external service, such as a database management system

 

Transparent persistence

A persistence solution that hides the underlying storage technology.

 

Declarative mapping schema

A structure that defines the relationships between domain classes in the object-oriented model and relations in the relational model

 

Advantages and Disadvantages of the SQL Query Mapping Frameworks

 

Advantages

•Direct access to all DBMS capabilities provided through SQL

•Mapping to legacy database schemas easier

•Amount of code required significantly less than with call-level APIs

•Database access code easier to manage than with call-level APIs

 

Disadvantages

•More overhead than with call-level APIs

•Developers need a detailed understanding of DBMS capabilities and the database schema

•SQL code not generated automatically

•The approach does not provide transparent persistence

 

Advantages and Disadvantages of the Call-Level API Approach

 

Advantages

•Low overhead

•Highest level of control over the details of the database connection

•Proliferation of code related to database connectivity

 

Disadvantages

•Need to write a lot of detailed code

•Little reuse of code

•Developers need a detailed understanding of DBMS capabilities and the database schema

•SQL code not generated automatically

•The approach does not provide transparent persistence

 

Advantages and Disadvantages of the Object-Relational Mapping Frameworks

 

Advantages

•They provide the highest level of persistence transparency

•Developers do not need to have a detailed understanding of the DBMS or the database schema

•The implementation of persistence is fully separated from the rest of the code

•They enable true object-oriented design

 

Disadvantages

•There is more overhead than with call-level APIs and with query mapping frameworks

•Complex cases often need detailed attention

•Legacy databases lead to difficulties

 

Typical Responsibilities of the Object-Relational Mapping Frameworks

•Provide a layer of abstraction between object-oriented applications and a database schema implemented with a DBMS leading to transparent persistence

•Generate SQL code for database access

•Centralize code needed to access the database

•Provide support for transaction integrity and management

•Provide services for concurrency control

•Provide a query language

 

HQL, the query language in Hibernate, resembles SQL in many ways and is sometimes used for the relational-object-oriented mapping, database creation and manipulation.