Skip Headers
Oracle® TopLink Developer's Guide
10g Release 3 (10.1.3.1.0)

Part Number B28218-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

34 Configuring a Relational Mapping

This chapter describes how to configure a relational mapping.

Table 34-1 lists the types of relational mappings that you can configure and provides a cross-reference to the type-specific chapter that lists the configurable options supported by that type.

Table 34-1 Configuring Relational Mappings

If you are creating... See...

Direct-to-Field Mapping


Chapter 35, "Configuring a Relational Direct-to-Field Mapping"


Transformation Mapping


Chapter 45, "Configuring a Relational Transformation Mapping"


Direct-to-XMLType Mapping


Chapter 36, "Configuring a Relational Direct-to-XMLType Mapping"


One-to-One Mapping


Chapter 37, "Configuring a Relational One-to-One Mapping"


Variable One-to-One Mapping


Chapter 38, "Configuring a Relational Variable One-to-One Mapping"


One-to-Many Mapping


Chapter 39, "Configuring a Relational One-to-Many Mapping"


Many-to-Many Mapping


Chapter 40, "Configuring a Relational Many-to-Many Mapping"


Aggregate Collection Mapping


Chapter 41, "Configuring a Relational Aggregate Collection Mapping"


Direct Collection Mapping


Chapter 42, "Configuring a Relational Direct Collection Mapping"


Direct Map Mapping


Chapter 44, "Configuring a Relational Direct Map Mapping"


Aggregate Object Mapping


Chapter 43, "Configuring a Relational Aggregate Object Mapping"



Table 34-2 lists the configurable options shared by two or more relational mapping types.

For more information, see the following:

Configuring Common Relational Mapping Options

Table 34-2 lists the configurable options shared by two or more relational mapping types. In addition to the configurable options described here, you must also configure the options described for the specific Relational Mapping Types, as shown in Table 34-1.

Configuring a Database Field

You can associate an object attribute with a database field.

Table 34-3 summarizes which relational mappings support this option.

Table 34-3 Relational Mapping Support for Database Field

Mapping Using TopLink Workbench
Using Java

Direct-to-Field Mapping


Supported.


Supported.


Direct-to-XMLType Mapping


Supported.


Supported.



When choosing the database field, you must consider Java and database field type compatibility.

TopLink supports the following Java types:

While executing reads, the mappings in Table 34-6 perform the simple one-way data conversions that Table 34-4 describes. For two-way or more complex conversions, You must use converters (see "Converters and Transformers").

The mappings in Table 34-3 also allow you to specify a null value. This may be required if primitive types are used in the object, and the database field allows null values. For more information, see "Configuring a Default Null Value at the Mapping Level".

Table 34-4 Type Conversions Provided by Direct-to-Field Mappings

Java type Database type

Integer, Float, Double, Byte, Short, BigDecimal, BigInteger, int, float, double, byte, short

NUMBER, NUMERIC, DECIMAL, FLOAT, DOUBLE, INT, SMALLINT, BIT, BOOLEAN

Boolean, boolean

BOOLEAN, BIT, SMALLINT, NUMBER, NUMERIC, DECIMAL, FLOAT, DOUBLE, INT

String

VARCHAR, CHAR, VARCHAR2, CLOB, TEXT, LONG, LONG VARCHAR, MEMO

The following types apply only to Oracle9: NVARCHAR2, NCLOB, NCHAR

byte[ ]

BLOB, LONG RAW, IMAGE, RAW, VARBINARY, BINARY, LONG VARBINARY

Time

TIME

sql.Date

DATE (only applies to DB2)

Timestamp, util.Date, Calendar

TIMESTAMP (only applies to DB2)

sql.Date, Time, Timestamp, util.Date, Calendar

To use oracle.sql.TimeStamp, see "Support for oracle.sql.TimeStamp".

DATE, DATETIME (applies to Oracle, Sybase, SQL Server)


Support for oracle.sql.TimeStamp

TopLink provides additional support for mapping Java date and time data types to Oracle database DATE, TIMESTAMP, and TIMESTAMPTZ data types when you use the Oracle JDBC driver with Oracle9i Database Server or later and the Oracle9Platform in TopLink.

In a direct-to-field mapping, you are not required to specify the database type of the field value; TopLink determines the appropriate data type conversion.

Table 34-5 lists the supported direct-to-field mapping combinations.

Table 34-5 Supported Oracle Database Date and Time Direct-to-Field Mappings

Java Type Database Type Description

java.sql.Time

TIMESTAMP

Full bidirectional support.


TIMESTAMPTZ

Full bidirectional support.


DATE

Full bidirectional support.

java.sql.Date

TIMESTAMP

Full bidirectional support.


TIMESTAMPTZ

Full bidirectional support.


DATE

Full bidirectional support.

java.sql.Timestamp

TIMESTAMP

Full bidirectional support.


TIMESTAMPTZ

Full bidirectional support.


DATE

Nanoseconds are not stored in the database.

java.util.Date

TIMESTAMP

Full bidirectional support.


TIMESTAMPTZ

Full bidirectional support.


DATE

Milliseconds are not stored in the database.

java.util.Calendar

TIMESTAMP

Native SQL or binding gives Calendar timezone.

Note: The TIMESTAMP database value has no timezone – the Calendar object provides the local timezone by default. If the database is not in this timezone, you must obtain the database timezone by some other means and update the Calendar object accordingly. For this reason, TIMESTAMPTZ may be a better choice.


TIMESTAMPTZ

Native SQL or binding stores timezone; standard SQL is based on the local timezone.


DATE

Neither timezone nor milliseconds are stored in the database.


Note that some of these mappings result in a loss of precision: avoid these combinations if you require this level of precision. For example, if you create a direct-to-field mapping between a java.sql.Date attribute and a TIMESTAMPTZ database field, there is no loss of precision. However, if you create a direct-to-field mapping between a java.sql.Timestamp attribute and a DATE database field, the nanoseconds or milliseconds of the attribute are not stored in the database.

Using TopLink Workbench

Use this procedure to select a specific database field for a direct mapping.

  1. Select the direct mapping attribute in the Navigator. Its properties appear in the Editor.

  2. Click the General tab. The General tab appears.

    Figure 34-1 Direct Mapping General Tab, Database Field Option

    Description of Figure 34-1 follows
    Description of "Figure 34-1 Direct Mapping General Tab, Database Field Option"

Use the Database Field field to select a field for this direct mapping. You must have previously associated the descriptor with a database table as described in "Configuring Associated Tables".


Note:

For direct to field mappings of a an Aggregate descriptor (see "Configuring a Relational Descriptor as a Class or Aggregate Type"), this field is for display only and cannot be changed.

Configuring Reference Descriptor

In relational mappings that extend oracle.toplink.mappings.ForeignReferenceMapping, attributes reference other TopLink descriptors–not the data source. You can select any descriptor in the project.

Table 34-6 summarizes which relational mappings support this option.

Using TopLink Workbench

To specify a reference descriptor for a relational mapping, use this procedure.

  1. Select the mapped attribute in the Navigator. Its properties appear in the Editor.

  2. Click the General tab. The General tab appears.

    Figure 34-2 General Tab, Reference Descriptor Field

    Description of Figure 34-2 follows
    Description of "Figure 34-2 General Tab, Reference Descriptor Field"

Use the Reference Descriptor field to select the descriptor referenced by this relationship mapping.


Note:

For aggregate mappings the Reference Descriptor must be an aggregate. See "Configuring a Relational Descriptor as a Class or Aggregate Type" for more information.

For variable one-to-one mappings, the Reference Descriptor must be an interface. See Chapter 38, "Configuring a Relational Variable One-to-One Mapping" for more information.


You can specify a reference descriptor that is not in the current TopLink Workbench project. For example, to create a mapping to an Employee class that does not exist in the current project, do the following:

  1. Add the Employee class to your current project. See "Working With Projects".

  2. Create the relationship mapping to the Employee descriptor.

  3. Deactivate the Employee descriptor. See "Active and Inactive Descriptors".

When you generate the deployment XML for your project, the mapping to the Employee class will be included, but not the Employee class.

Configuring Batch Reading

Batch reading can be used in most of the relational mappings. This feature should be used only if it is known that the related objects are always required with the source object.

Table 34-7 summarizes which relational mappings support this option.

Using TopLink Workbench

To use batch reading in a relationship mapping, use this procedure:

  1. Select the mapped attribute in the Navigator. Its properties appear in the Editor.

  2. Click the General tab. The General tab appears.

    Figure 34-3 General Tab, Batch Reading Option

    Description of Figure 34-3 follows
    Description of "Figure 34-3 General Tab, Batch Reading Option"

To specify that this mapping using batch reading, select the Batch Reading option.

Using Java

Example 34-1 Query Optimization Using Batching

The following code example illustrates using batch for query optimization.

// Queries on Employee are configured to always batch read Address
OneToManyMapping phoneNumbersMapping = new OneToManyMapping();
phoneNumbersMapping.setReferenceClass(" PhoneNumber.class")
phoneNumbersMapping.setAttributeName("phones");
phoneNumbersMapping.useBatchReading();
phoneNumbersMapping.privateOwnedRelationship();

Configuring Query Key Order

You can configure TopLink to maintain collections in order by query key.

Table 34-8 summarizes which relational mappings support this option.

Table 34-8 Relational Mapping Support for Query Key Order

Mapping Using TopLink Workbench
Using Java

Variable One-to-One Mapping


Supported.


Supported.


One-to-Many Mapping


Supported.


Supported.


Aggregate Collection Mapping


Unsupported.


Supported.



Using TopLink Workbench

To specify the order of a mapping's query keys, use this procedure:

  1. Select the mapped attribute in the Navigator. Its properties appear in the Editor.

  2. Click the Ordering tab. The Ordering tab appears.

Field Description
Query Key Specify the query key to order by.

Click Add to add query keys to, or Remove to remove query keys from the ordering operation.

Click Up or Down to change the sort order of selected query keys.

Order Specify if TopLink orders the selected query key in Ascending or Descending (alphabetical) order.

Configuring Table and Field References (Foreign and Target Foreign Keys)

A foreign key is a combination of one or more database columns that reference a unique key, usually the primary key, in another table. Foreign keys can be any number of fields (similar to a primary key), all of which are treated as a unit. A foreign key and the parent key it references must have the same number and type of fields.

Mappings that extend oracle.toplink.mappings.ForeignReferenceMapping use foreign keys to find information in the database so that the target object(s) can be instantiated. For example, if every Employee has an attribute address that contains an instance of Address (which has its own descriptor and table) then, the one-to-one mapping for the address attribute would specify foreign key information to find an Address for a particular Employee.

TopLink classifies foreign keys into two categories in mappings–foreign keys and target foreign keys:

The table reference is the database table that contains the foreign key references.

Table 34-9 summarizes which relational mappings support this option.

Using TopLink Workbench, you can either import this table from your database or create it. If you import tables from the database (see "Importing Tables From a Database"), TopLink creates references that correspond to existing database constraints (if supported by the driver). You can also define references in TopLink without creating similar constraints on the database.

Using TopLink Workbench

To specify a table for a mapping reference, use this procedure:

  1. Select the mapped attribute in the Navigator. Its properties appear in the Editor.

  2. Click the Table Reference tab. The Reference tab appears.

    Figure 34-5 Table Reference Tab, Table Reference Field

    Description of Figure 34-5 follows
    Description of "Figure 34-5 Table Reference Tab, Table Reference Field"

Use the following information to select the field references on the tab:

Field Description
Table Reference Select an existing table, or click New to create a new table reference.
Source and Target Field Click Add to create new foreign key reference.

To delete an existing key pair reference, select the Source and Target fields and click Remove.

    Source Field Select the database field from the source table for this foreign key reference.
    Target Field Select the database field from the target table for this foreign key reference.
    Target Foreign Key Specify whether or not the reference is from the target object's table back to the key from the mapping's descriptor's table.