Oracle Application Server TopLink Mapping Workbench User's Guide 10g (9.0.4) Part Number B10316-01 |
|
In Oracle Application Server 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 OracleAS TopLink:
There are two basic ways of storing object attributes directly in a database table:
OracleAS TopLink furnishes 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, then 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 the database, it stores the value of the attribute in a field of the table column. OracleAS 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.
Figure 5-1 illustrates a direct-to-field mapping between the Java attribute city
and the relational database column CITY
. Similarly, direct-to-field mappings could be defined from country
to COUNTRY
, id
to ADDRESS_ID
, established
to EST_DATE
, and province
to PROVINCE
.
Text description of the illustration dtfmpfig.gif
Use this procedure to create a basic direct-to-field mapping to map a Java attribute directly to a value in a database.
Text description of the illustration dtfmpbtn.gif
on the mapping toolbar.
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.
For Oracle9 databases, OracleAS TopLink supports NCHAR, NAVRCHAR2, and NCLOB database types. Use the Ncharacter
, NString
, and NClob
target types, respectively.
Figure 5-3 illustrates a type conversion mapping. Because the java.util.Date
class is stored by default as a Timestamp
in the database, it must first be converted to an explicit database type such as java.sql.Date
(required only for DB2--most other databases have a single date data-type that can store any date or time).
Text description of the illustration tcmapfig.gif
Use this procedure to create a type conversion mapping.
Text description of the illustration tcmapbtn.gif
on the mapping toolbar.
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
.
Text description of the illustration obmapfig.gif
Use this procedure to create an object type mapping between an attribute and a database column.
Text description of the illustration obmapbtn.gif
on the mapping toolbar. The Object type mapping tab appears in the Editor pane.
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.
As with direct-to-field mappings, serialized object mappings require you to specify an attribute and field, as shown in the following illustration.
Text description of the illustration serobjfg.gif
Use this procedure to create serialized object mappings.
Text description of the illustration srmapbtn.gif
on the mapping toolbar.
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 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.
Text description of the illustration trmapfig.gif
Use this procedure to create transformation mappings in the OracleAS TopLink Mapping Workbench.
Text description of the illustration trmapbtn.gif
from the Mapping toolbar.To remove a transformation method, select the method and click 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 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.
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"; }
Refer to "Amending Descriptors After Loading" for more information.
|
![]() Copyright © 1997, 2003 Oracle Corporation. All Rights Reserved. |
|