Oracle9iAS TopLink Mapping Workbench Reference Guide Release 2 (9.0.3) Part Number B10063-01 |
|
In TopLink, direct mappings define how a persistent object refers to objects without descriptors, such as the JDK classes and primitives.
You can create the following direct mappings in TopLink:
There are two basic ways of storing object attributes directly in a database table:
TopLink provides the following classes of direct mappings:
If the application's objects contain attributes that cannot be represented as direct-to-field, type conversion, or object-type mappings, the application must provide transformation routines for saving the attributes.
If a direct-to-field mapping cannot be used to perform the desired conversion, try type conversion and object type mappings before attempting to define a custom transformation mapping.
Direct-to-field mappings map a Java attribute directly to a value database column. When the application writes a Java instance to database, it stores the value of the attribute in a field of the table column. TopLink supports the following types:
java.lang
: Boolean
, Float
, Integer
, String
, Double
, Long
, Short
, Byte
, Byte[ ]
, Character
, Character[ ]
; all of the primitives associated with these classes
java.math
: BigInteger
, BigDecimal
java.sql
: Date
, Time
, Timestamp
java.util
: Date
, Calendar
While reading, direct-to-field mappings perform some simple one data conversions, as described in Table 5-1. You must use other direct mappings for two-way or more complete conversions.
Direct-to-field mappings 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.
Use this procedure to create a basic direct-to-field mapping to map a Java attribute directly to a value in a database.
You can also specify:
Type conversion mappings explicitly map a database type to a Java type. For example, a Number
in the database can be mapped to a String
in Java, or a java.util.Date
in Java can be mapped to a java.sql.Date
in the database.
Use this procedure to create a type conversion mapping.
You can also specify:
Object type mappings match a fixed number of database values to Java objects. Use these mappings when the values in the database differ from those in Java. Object types mappings are similar to direct-to-field mappings in all other respects.
The following figure illustrates an object type mapping between the Employee
attribute gender
and the relational database column GENDER
. If the gender
value in the Java class = Male
, the system stores it in the GENDER
database field as M
; Female
is stored as F
.
Use this procedure to create an object type mapping between an attribute and a database column.
To remove a database value, select the value and click Remove.
You can also specify:
Serialized object mappings are used to store large data objects, such as multimedia files and BLOBs, in the database. Serialization transforms these large objects as a stream of bits.
Like direct-to-field mappings, serialized object mappings require an attribute and field to be specified, as illustrated in the following illustration.
Use this procedure to create serialized object mappings.
You can also specify:
Use transformation mappings for specialized translations between how a value is represented in Java and in the database.
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 may specify a second parameter, when 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-7 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 Mapping Workbench.
To remove a transformation method, select the method and click on Remove.
You can also specify:
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 may be mapped from a computed value that does not map to a logical attribute. This, in effect, constitutes a write-only mapping. In the 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, simply create an instance of TransformationMapping
and send addFieldTransformation()
message for each field to be written.
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 a 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"; }
Refer to "Amending Descriptors After Loading" for more information.
|
Copyright © 2002 Oracle Corporation. All Rights Reserved. |
|