Creating Transformation Mappings

Use this procedure to create transformation mappings.

To Create a Transformation Mapping:

  1. In the Structure window, select the attribute to be mapped.
  2. Enter the necessary information to complete the mapping.
  3. Use the Database Row --> Object Method drop-down list to select a method to convert the database row into an object.
    Note: The method must have the parameter (DatabaseRow) or parameters (DatabaseRow, Session).
  4. Click Add to add field transformation methods to the descriptor.
    To remove a transformation method, select the method and click Remove.

Related Topics

About CMP EJB Mappings

 

Working with CMP Direct Mappings

 

Working with Transformation Mappings
Working with Common Mapping Properties
Specifying Advanced Transformation Mapping Features by Amending the Descriptor

Example 6-1 Transformation Mapping Code Example

The following code example illustrates the methods required for a transformation mapping:

// Get method for the normalHours attribute since method access indicated
access public Time[] getNormalHours()
{
    return normalHours;
}
// Set method for the normalHours attribute since method access indicated
access public void setNormalHours(Time[] theNormalHours)
{
    normalHours = theNormalHours;
}
// Create attribute transformation method to read from the database row
//** Builds the normalHours Vector. IMPORTANT: This method builds the value but does not set it. The mapping will set it using method or direct access as defined in the descriptor. */
public Time[] getNormalHoursFromRow(DatabaseRow row)
{
    Time[] hours = new Time[2];
    hours[0] = (Time)row.get("START_TIME");
    hours[1] = (Time)row.get("END_TIME");
    return hours;
}
// Define a field transformation method to write out the start time. Return the first element of the normalHours attribute.
public java.sql.Time getStartTime()
{
    return getNormalHours()[0];
}
// Define a field transformation method to write out the end time. Return the last element of the normalHours attribute.
public java.sql.Time getEndTime()
{
    return getNormalHours()[1];
}

Specifying Advanced Transformation Mapping Features
by Amending the Descriptor

In TopLink, transformation mappings do not require you to specify an attribute.

A field can be mapped from a computed value that does not map to a logical attribute. This, in effect, constitutes a write-only mapping. In the TopLink Mapping editor, all mappings are associated with an attribute before any other information can be specified. Therefore, to use a write-only mapping, you must build it by amending the descriptor. The mapping itself has no attribute name, get and set methods, or attribute method. In your amendment method, create an instance of TransformationMapping and send addFieldTransformation() message for each field to be written.

Example 6-2 Descriptor Amendment Examples

The following code example illustrates creating a write-only transformation mapping and adding it to the descriptor.

public static void addToDescriptor(Descriptor descriptor) {
// Create a Transformation mapping and add it to the descriptor.
TransformationMapping transMapping = new transMapping.addFieldTransformation("WRITE_DATE", "descriptor.addMapping(transMapping);
}

The following example illustrates how to create a one-way transformation mapping by using the inheritance indicator field of the primary key. Map the class as normal, including the other part of the primary key, and the inheritance through the type field.

Create an amendment method for the class:

public void addToDescriptor(Descriptor descriptor) {
    TransformationMapping keyMapping = new TransformationMapping();
    keyMapping.addFieldTranslation("PROJECT.PROJ_TYPE",
        "getType");descriptor.addMapping(keyMapping);}
  

Define the getType method on the class to return its type value:

Project>>public abstract String getType();
LargeProject>>public String getType() { return "L"; }
SmallProject>>public String getType() { return "S"; }


Related Topics

About CMP EJB Mappings

 

Working with CMP Direct Mappings

 

Working with Transformation Mappings
Creating Transformation Mappings

 

Copyright © 1997, 2004, Oracle. All rights reserved.