Skip Headers
Oracle® Application Development Framework Developer's Guide
10g Release 3 (10.1.3)
B25386-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
 

3.4 Mapping Classes to Tables

One of the greatest strengths of TopLink is its ability to transform data between an object representation and a representation specific to a data source. This transformation is called mapping and it is the core of a TopLink project.

A mapping corresponds to a single data member of a domain object. It associates the object data member with its data source representation and defines the means of performing the two-way conversion between object and data source.

TopLink uses the metadata produced by Mapping editor to describe how objects and beans map to the data source. This approach isolates persistence information from the object model—developers are free to design their ideal object model and DBAs are free to design their ideal schema.

3.4.1 Types of Mappings

Within ADF, TopLink supports relational and object-relational mappings.

  • Relational Mappings – Mappings that transform any object data member type to a corresponding relational database (SQL) data source representation in any supported relational database. Relational mappings allow you to map an object model into a relational data model.

  • Object-Relational Mappings – Mappings that transform certain object data member types to structured data source representations optimized for storage in specialized object-relational databases such as Oracle Database. Object-relational mappings allow you to map an object model into an object-relational data model.

3.4.2 Direct Mappings

You can create the following direct mappings in TopLink:

  • Direct-to-field mappings – Map a Java attribute directly to a database field.

  • Type conversion mappings – Map Java values with simple type conversions, such as character to string.

  • Object type mappings – Use an association to map values to the database.

  • Serialized object mappings – Map serializable objects, such as multimedia objects, to database BLOB fields.

  • Transformation mappings – Allow you to create custom mappings where one or more fields can be used to create the object be stored in the attribute.

3.4.3 How to Create Direct Mappings

To map create Java classes directly to database tables, select the Java attribute in the TopLink Map – Structure window. Oracle JDeveloper displays a list of the available mappings for the selected attribute (as shown in Figure 3-9).

Figure 3-9 Mapping Editor

welcome screen from Creating Java Objects from Tables wizard
Description of "Figure 3-9 Mapping Editor"

You can also use TopLink Automap feature to automatically map the attributes in a specific Java class or package. Refer to the Oracle JDeveloper online help for more information.

3.4.4 What Happens when you Create a Direct Mapping

Example 3-4 illustrates the Java code that Oracle JDeveloper generates when you create a direct-to-field direct mapping. In this example, the description attribute of the Products class maps directly to a field on the database table.

Example 3-4 Java Code for a Direct Mapping

...
package oracle.srdemo.model;
public class Products {

  private String description;

    public String getDescription() {
      return this.description;
    }

    public void setDescription(String description) {
      this.description = description;
    }
}


3.4.5 What You May Need to Know

Use the Mapping editor to customize the TopLink mappings. Some common customizations for direct mappings include:

  • Specifying the mapping as "read only." These mappings will not be included during update or delete operations.

  • Using custom get and set methods.

  • Defining a default value. This value will be used if the actual field in the database is null.

Figure 3-10 shows the General tab of a direct-to-field mapping in the Mapping editor. Each direct mapping (see Section 3.4.2, "Direct Mappings") may have additional, specific options as well. Refer to the Oracle JDeveloper online help for more information.

Figure 3-10 Sample DIrect-to-Field Mapping

welcome screen from Creating Java Objects from Tables wizard
Description of "Figure 3-10 Sample DIrect-to-Field Mapping"