Oracle® Application Server TopLink Mapping Workbench User's Guide
10g Release 2 (10.1.2) Part No. B15900-01 |
|
Previous |
Next |
Use transformation mappings for specialized translations between how a value is represented in Java and in the database.
Tip: Use transformation mappings only when mapping multiple fields into a single attribute. Because of the complexity of transformation mappings, it is often easier to perform the transformation withget /set methods of a direct-to-field mapping.
|
Often, a transformation mapping is appropriate when values from multiple fields are used to create an object. This type of mapping requires that you provide an attribute transformation method that is invoked when reading the object from the database. This method must have at least one parameter that is an instance of DatabaseRow
. In your attribute transformation method, you can send the get()
message to the DatabaseRow
to get the value in a specific column. Your attribute transformation method can specify a second parameter, when it is an instance of Session
. The Session
performs queries on the database to get additional values needed in the transformation. The method should return the value to be stored in the attribute.
Transformation mappings also require a field transformation method for each field, to be written to the database when the object is saved. The transformation methods are specified in a dictionary associating each field with a method. The method returns the value to be stored in that field.
Figure 5-9 illustrates a transformation mapping. The values from the B_DATE and B_TIME fields are used to create a java.util.Date to be stored in the birthDate attribute
Use this procedure to create transformation mappings in the OracleAS TopLink Mapping Workbench.
To create a transformation mapping:
In the Navigator pane select the attribute to be mapped.
Description of the illustration trmapbtn.gif
Click the Transformation Mapping button from the Mapping toolbar.
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) .
|
Click Add to add field transformation methods to the descriptor.
To remove a transformation method, select the method and click Remove.
Select the Use indirection option to specify if the creation of the target object requires extensive computational resources. If selected, OracleAS TopLink uses indirection objects. See "Working with Indirection" on page 6-5 for more information.
After specifying the details of the mapping, create the attribute field transformation methods in the associated Java class (see Example 5–3).
You can also specify:
Read-only attributes – See "Specifying Read-Only Settings" on page 4-72
Access methods – See "Specifying Direct Access and Method Access" on page 4-71
Example 5-3 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 OracleAS 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 OracleAS TopLink Mapping Workbench, 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 5-4 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.
Note: The OracleAS TopLink Mapping Workbench displays a neediness error, because the class indicator field part of the primary key is not mapped. Use the following code to create an amendment method to map the indicator field. |
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Ó; }
Refer to "Amending Descriptors After Loading" on page 4-27 for more information.