トランスフォーメーション・マッピングを作成するにはこの手順を使用します。
トランスフォーメーション・マッピングを作成するには、次のようにします。
図6-5 「トランスフォーメーション・マッピング」-「一般」
(DatabaseRow)
またはパラメータ(DatabaseRow, Session)
が必要です。
例6-1 トランスフォーメーション・マッピングのコード例
次のコード例は、トランスフォーメーション・マッピングに必要なメソッドを示しています。
// 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];
}
TopLinkでは、トランスフォーメーション・マッピングで属性を指定する必要はありません。
フィールドは、計算された値からマップされ、この値は論理的属性にマップしません。これは事実上、書込み専用マッピングを構成します。TopLinkマッピング・エディタでは、その他の情報が指定される前にすべてのマッピングが属性と関連付けられています。このため、書込み専用マッピングを使用する場合は、ディスクリプタを修正してビルドする必要があります。マッピング自体には属性名、get
メソッド、set
メソッドまたは属性メソッドはありません。修正メソッドでは、TransformationMapping
のインスタンスを作成し、書き込まれる各フィールドに対するaddFieldTransformation()
メッセージを送信します。
例6-2 ディスクリプタの修正例
次のコード例は、書込み専用トランスフォーメーション・マッピングの作成とそのマッピングのディスクリプタへの追加を示しています。
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);
}
次の例は、主キーの継承インジケータ・フィールドの使用による一方向トランスフォーメーション・マッピングの作成方法を示しています。主キーの他の部分も含めてクラスを通常どおりマップし、型フィールドを介して継承をマップします。
クラスの修正メソッドを作成します。
public void addToDescriptor(Descriptor descriptor) {
TransformationMapping keyMapping = new TransformationMapping();
keyMapping.addFieldTranslation("PROJECT.PROJ_TYPE",
"getType");descriptor.addMapping(keyMapping);}
型の値を返すようにクラスのgetType
メソッドを定義します。
Project>>public abstract String getType();
LargeProject>>public String getType() { return "L"; }
SmallProject>>public String getType() { return "S"; }
Copyright © 1997, 2006, Oracle. All rights reserved.