Skip Headers
Oracle® Application Server TopLink Application Developer's Guide
10g Release 2 (10.1.2)
Part No. B15901-01
  Go To Documentation Library
Home
Go To Product List
Solution Area
Go To Table Of Contents
Contents
Go To Index
Index

Previous
Previous
Next
Next
 

Introduction to Mapping Concepts

In an OracleAS TopLink application, you persist objects by storing, or mapping, information about them in a relational database. A mapping has three components:

Although OracleAS TopLink supports more complex mappings, most OracleAS TopLink classes map to a single database table that defines the type of information available in the class. Each object instantiated from a given class maps to a single row comprising the object attributes, plus an identifier (the primary key) that uniquely identifies the object.

Figure 3-1 How Classes and Objects Map to a Database Table

Description of ezmap.gif follows
Description of the illustration ezmap.gif

Figure 3-1 illustrates the simplest case, in which:

OracleAS TopLink provides you with the tools to build these mappings—from the simple mappings illustrated in Figure 3-1 to complex mappings. OracleAS TopLink addresses the most difficult challenge for mapping—transforming a class or object into a database table or row.

The following section describes the basic concepts that you must understand before moving on to the more in-depth information in this chapter, and introduces some of the more complex issues that are part of mapping.

Persistent Entities

Persistent entities are entities that survive, or persist, beyond the scope of a given transaction. A key feature of OracleAS TopLink is its ability to persist objects and entities in an application by mapping them to a database.

Metadata Model

OracleAS TopLink implements a metadata model, in which OracleAS TopLink uses metadata to define how objects and classes map to tables or rows, as well how tables and rows map to objects and classes. OracleAS TopLink uses the metadata, contained in the descriptor, to generate SQL statements that create, read, modify, and delete objects.

The OracleAS TopLink metadata model has three levels of information:

  • Mappings describe how individual object attributes relate to the fields in a database row. Mappings relate object attributes to the database at the row level, and can involve a complex transformation or a direct entry.

    For more information, see Primitive Versus Complex Data.

  • Descriptors describe how a class relates to a database table. Class attributes map to database columns. Descriptors relate object classes to the database at the table level.

  • Projects are collections of descriptors that make up an OracleAS TopLink application. Projects relate groups of object classes to the database at the schema level.

The metadata model describes the simplest case. More complex cases in which objects map to partial or multiple rows, and classes map to multiple tables, are described later in this chapter. For the purposes of introducing mapping, this simple case forms the basis for understanding how mapping works.

OracleAS TopLink interaction with both object models and databases is unintrusive: OracleAS TopLink adapts to the object model and database schema, rather than requiring you to design your object model or database schema to suit OracleAS TopLink.

OracleAS TopLink Mapping Workbench

OracleAS TopLink Mapping Workbench is a graphical tool that gives you access to most OracleAS TopLink features. Although OracleAS TopLink Mapping Workbench does not support the complete OracleAS TopLink feature set, it does support the basic functions required for mapping your application, as well as most of the advanced features.

The graphical nature of OracleAS TopLink Mapping Workbench makes it easy to create models and mappings. As such, Oracle recommends that you build as much of your project as possible in OracleAS TopLink Mapping Workbench.

An important feature of OracleAS TopLink Mapping Workbench is its ability to generate deployment files from your project, either as deployment XML files or Java source code.

For more information about generating deployment files, see "Exporting Project Information" in the Oracle Application Server TopLink Mapping Workbench User's Guide.

Deployment XML Generation

OracleAS TopLink Mapping Workbench can generate XML files from your project. OracleAS TopLink reads these files at runtime to configure your application. Deployment XML files reduce development time by eliminating the need to regenerate and recompile Java code each time the project changes.

Project Class Generation

OracleAS TopLink Mapping Workbench can generate Java source files for your project that you compile and run for your application. Often, this generated code deploys faster than XML files, but is less flexible and more difficult to troubleshoot.

OracleAS TopLink Mapping Types

OracleAS TopLink offers several types of mapping, each optimized for different types of information.

Direct Mappings

Direct mappings define how a persistent object refers to objects and attributes that do not have OracleAS TopLink descriptors, such as the JDK classes, primitive types, and other nonpersistent classes. Direct mappings map primitive data types to database data types on a one-to-one basis.

For more information about direct mappings, see "Direct Mappings".

Relationship Mappings

Relationship mappings describe how you manage relationships on the database. OracleAS TopLink uses several different mechanisms to represents relationships in the database, the most common of which is foreign keys. The OracleAS TopLink descriptors include details on the storage and retrieval mechanisms used for the relationship.

For more information about relationship mappings, see "Relationship Mappings".

Inheritance

In object modeling, when one class (the superclass) shares its attributes with another class (the subclass), the subclass is said to inherit those attributes from the superclass or table. Similarly, in the database world, when one table shares information with a subordinate table in the database, the subordinate table inherits information from the main table. Although these two types of inheritance are similar, mapping them properly can be difficult.

OracleAS TopLink supports both object and database inheritance, and enables you to easily map object inheritance to database tables. OracleAS TopLink treats both types of inheritance interchangeably, provided that you map the inheritance in the class descriptors for the superclass and subclass.

For more information about inheritance, see "Inheritance".

Objects and the Database

OracleAS TopLink stores objects in database tables. In most cases, a single row in a database table represents a single object in your OracleAS TopLink application. Several OracleAS TopLink concepts follow from this arrangement, including:

  • Primary Keys

  • Sequencing

  • Foreign Keys and Object Relationships

Primary Keys

A primary key is a column, or a combination of columns, in a database table that contains a unique identifier for every record in the table. Persistent objects require a primary key. If a table uses a combination of columns to create a unique identifier, this combination of fields is collectively called a composite primary key. In either case, a primary key uniquely identifies each row.

Sequencing

Sequencing is a mechanism to populate the primary key attribute of new objects and entity beans before inserting them into the database.

For more information, see "Sequencing".

Foreign Keys and Object Relationships

Objects stored in one database table (the source objects) can share a relationship with objects in other tables (the target objects). To define these relationships, your tables must include data that identifies which target objects are related to the source object in the relationship.

The target table primary key in the relationship becomes a foreign key in the source table and identifies which objects in the target table are related to the objects in the source table.

For more information, see "Foreign Keys".

Indirection

The standard object reading behavior in Java is that when you read an object, you also read all its related objects, which can be unnecessarily time consuming. The OracleAS TopLink indirection feature enables you to defer reading related objects until they are required. This is also known as lazy reading, lazy loading, and just-in-time reading.

For more information, see "Indirection".

Serialization

In OracleAS TopLink, serialization is the act of writing out (marshalling) an object from its home OracleAS TopLink Java Virtual Machine (JVM) to another JVM.

For more information, see "Serialization".

General Terms and Concepts

This section outlines some of the more common general concepts you will encounter when dealing with mappings.

Primitive Versus Complex Data

OracleAS TopLink treats certain classes as primitive data types for mapping purposes. These include Strings and Integers. Primitive data types correspond directly to representations in the database fields in which they are stored.

Because of this direct correspondence, there is no need to describe how to map the primitive data. As a result, OracleAS TopLink does not require mapping descriptors for primitive data types.

Object attributes represent complex data. OracleAS TopLink requires class descriptors to define how the attributes and relationships of instances of a particular class are stored and retrieved. Descriptors specify where and how attributes are stored in database tables.

Java Objects

Java objects represent the components or business logic of your application. As the basic building blocks in an OracleAS TopLink application, objects can include data, methods, relationships, and inheritance hierarchies.