Oracle Fusion Middleware Developer's Guide for Oracle TopLink 11g Release 1 (11.1.1) Part Number B32441-03 |
|
|
View PDF |
This chapter describes the various components that you must configure in order to use a relational variable one-to-one mapping.
This chapter includes the following sections:
For information on how to configure TopLink mappings options common to two or more mapping types, see Chapter 121, "Configuring a Mapping".
For information on how to create TopLink mappings, see Chapter 120, "Creating a Mapping".
Table 32-1 lists the configurable options for a relational variable one-to-one mapping.
Table 32-1 lists the configurable options for a relational variable one-to-one mapping.
Table 32-1 Configurable Options for Relational Variable One-to-One Mapping
Option | Oracle JDeveloper |
TopLink Workbench |
Java |
---|---|---|---|
![]() |
![]() |
![]() |
|
Section 121.6, "Configuring Method or Direct Field Accessing at the Mapping Level" |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|
Section 121.7, "Configuring Private or Independent Relationships" |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|
![]() |
![]() |
![]() |
|
![]() |
![]() |
![]() |
|
![]() |
![]() |
![]() |
|
![]() |
![]() |
![]() |
Example 32-1 shows how to create a variable one-to-one mapping and add it to a descriptor using Java code.
Example 32-1 Variable One-to-One Mapping
public void customize(ClassDescriptor descriptor) { VariableOneToOneMapping mapping = new VariableOneToOneMapping(); // configure mapping ... // add mapping to descriptor descriptor.addMapping(mapping); }
For more information, see the following:
In variable one-to-one mappings, you can use an indicator column in the source table to specify the correct target table, as illustrated in Figure 32-1. This section includes information on implementing class indicators.
A source table has an indicator column that specifies the target table through the class indicator field.
Figure 32-1 illustrates the EMPLOYEE
table that has a TYPE
column that indicates the target for the row (either PHONE
or EMAIL
).
Figure 32-1 Variable One-to-One Mapping using Class indicator Field
The principles of defining such a variable class relationship are similar to defining a normal one-to-one relationship, except:
The reference class is a Java interface, not a Java class. However, the method to set the interface is identical.
You must specify a type indicator field.
You specify the class indicator values on the mapping so that mapping can determine the class of object to create.
You must specify the foreign key names and the respective abstract query keys from the target interface descriptor.
Alternatively, you can use unique primary keys to specify the correct target. See Section 32.3, "Configuring Unique Primary Key" for more information.
To specify the class indicators for a variable one-to-one mapping, use this procedure:
Select the variable one-to-one mapping in the Navigator. Its attributes appear in the Editor.
Click the Class Indicator Info tab. The Class Indicator Info tab appears.
Use the Class Indicator Field to select the field on the database table (associated with the mapping's descriptor) to use as the indicator for the variable mapping.
Use the Indicator Type to specify the data type of the class indicator field by selecting the data type from the list.
To specify the specific class indicator field values for each (nonabstract) child class, click Edit and enter the appropriate value for each child class.
Example 32-2 illustrates how to define a variable one-to-one mapping using a class (type) indicator in Java code.
Example 32-2 Defining Variable One-to-One Mapping Using a Class Indicator
public void customize(ClassDescriptor descriptor) { VariableOneToOneMapping variableOneToOneMapping = new VariableOneToOneMapping(); variableOneToOneMapping.setAttributeName("contact"); variableOneToOneMapping.setReferenceClass (Contact.class); variableOneToOneMapping.setForeignQueryKeyName ("C_ID", "id"); variableOneToOneMapping.setTypeFieldName("TYPE"); // configure class indicators variableOneToOneMapping.addClassIndicator(Email.class, "Email"); variableOneToOneMapping.addClassIndicator(Phone.class, "Phone"); variableOneToOneMapping.dontUseIndirection(); variableOneToOneMapping.privateOwnedRelationship(); // add mapping to descriptor descriptor.addMapping(variableOneToOneMapping); }
For more information about the available methods for VariableOneToOneMapping
, see the Oracle Fusion Middleware Java API Reference for Oracle TopLink.
In variable one-to-one mappings, you can use a unique primary key in the source table to specify the correct target table, as illustrated in Figure 32-4. This section includes information on implementing class indicators.
Alternatively, you can use a class indicator to specify the correct target. See Section 32.2, "Configuring Class Indicator" for more information.
To specify the variable one-to-one mapping with a primary key, use this procedure:
Select the variable one-to-one mapping in the Navigator. Its attributes appear in the Editor.
Click the Class Indicator Info tab. The Class Indicator Info tab appears.
Figure 32-3 Class Indicator Info Tab, Configuring Primary Key
Use the Class Indicator Field to select none.
Use the Indicator Type to select none.
Use the Indicator Value column to specify none.
After choosing the reference descriptor for the mapping, deselect the Include check boxes.
Note:
You cannot deselect the value in the Class Indicator Field, unless the foreign key values in the source table are unique.Example 32-3 illustrates how to define a variable one-to-one mapping using a unique primary key in Java code.
Example 32-3 Defining Variable One-to-One Mapping Using a Unique Primary Key
public void customize(ClassDescriptor descriptor) {
VariableOneToOneMapping variableOneToOneMapping =
new VariableOneToOneMapping();
variableOneToOneMapping.setAttributeName("contact");
variableOneToOneMapping.setReferenceClass(Contact.class);
variableOneToOneMapping.setForeignQueryKeyName("C_ID", "id");
variableOneToOneMapping.dontUseIndirection();
variableOneToOneMapping.privateOwnedRelationship();
// add mapping to descriptor
descriptor.addMapping(variableOneToOneMapping);
}
For more information about the available methods for VariableOneToOneMapping
, see the Oracle Fusion Middleware Java API Reference for Oracle TopLink.
As Figure 32-4 illustrates, the value of the foreign key in the source table (C_ID
) mapped to the primary key of the target table is unique. No primary key values among the target tables are the same, so primary key values are not unique just in the table, but also among the tables.
Figure 32-4 Variable One-to-One Relationship with Unique Primary Key
If there is no indicator stored in the source table, TopLink cannot determine to which target table the foreign key value is mapped. Therefore, TopLink reads through all the target tables until it finds an entry in one of the target tables. This is an inefficient way of setting up a relation model. The class indicator is much more efficient as it reduces the number of reads performed on the tables to get the data. In the class indicator method, TopLink knows exactly which target table to look into for the data.
The principles of defining such a variable class relationship are similar to defining class indicator variable one-to-one relationships, except the following:
A type indicator field is not specified.
The class indicator values are not specified.
The type indicator field and its values are not needed, because TopLink goes through all the target tables until data is finally found.
This section includes information on configuring query key associations using development tools, as well as Java.
To specify the query keys used for a variable one-to-one mapping, use this procedure:
Select the variable one-to-one mapping in the Navigator. Its attributes appear in the Editor.
Click the Query Key Associations tab. The Query Key Associations tab appears
Use the following information to enter data in each field on the tab:
Use the Indicator Type to specify the data type of the class indicator field by selecting the data type from the list.
Field | Description |
---|---|
Foreign Key | The field from the database table to use as the foreign key in this relationship. |
Query Key Name | The name of the query key (from the referenced descriptor) for this association. See Section 119.10, "Configuring Query Keys" for more information. |
The API to configure query key associations is oracle.toplink.mappings.VariableOneToOneMapping
method addForeingQueryKeyName(String, String)
.
For more information about the available methods for VariableOneToOneMapping
, see the Oracle Fusion Middleware Java API Reference for Oracle TopLink.