Use this procedure to create transformation mappings.
To Create a Transformation Mapping:
Figure 6-5 Transformation Mapping General
(DatabaseRow)
or parameters (DatabaseRow, Session)
.
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];
}
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"; }
Copyright © 1997, 2004, Oracle. All rights reserved.