@Repeatable(value=Converts.class) @Target(value={METHOD,FIELD,TYPE}) @Retention(value=RUNTIME) public @interface Convert
Basicアノテーションや対応するXML要素を使用する必要はありません。
ConvertアノテーションはId属性、バージョン属性、リレーションシップ属性、列挙型または時制が明示的に指定された属性の変換を指定するために使用すべきではありません。
そのような変換を指定するアプリケーションはポータブルではありません。
Convertアノテーションは基本属性または基本タイプの要素コレクションに適用できます。(この場合コンバーターはコレクションの要素に適用されます)
このような使用方法の場合はattributeName要素を指定してはいけません。
Convertアノテーションは組み込み属性やキーまたは値が組み込み型のMapのコレクション属性に適用できます。
(この場合コンバーターはコレクションに含まれる組み込みインスタンスの指定された属性に適用されます)
このような使用方法の場合はattributeName要素を指定する必要があります。
複数の組み込みレベルで変換マッピングを上書きするためには、組み込み属性内の属性を示すためにattributeName要素にドット(".")表記を使用する必要があります。
ドット表記で使用される各識別子の値はそれぞれの組み込みフィールドまたはプロパティの名前です。
Convertアノテーションを組み込みクラスのインスタンスに含まれるMapに適用する場合はattributeName要素は指定されなければならず、
マップのキーまたはマップの値の一部として指定するために"key."もしくは"value."を変換される属性の名前にプレフィックスとして使用する必要があります。
Convertアノテーションを基本型のMapのキーの変換を指定するためにMapに適用する場合はattributeName要素の値として"key"を使用して変換されるのはマップのキーであることを指定する必要があります。
Convertアノテーションはマップドスーパークラスを拡張したエンティティクラスに対して継承された基本属性または組み込み属性の変換マッピングを指定または上書きするために適用できます。
Example 1: 基本属性の変換
@Converter
public class BooleanToIntegerConverter
implements AttributeConverter<Boolean, Integer> { ... }
@Entity
public class Employee {
@Id long id;
@Convert(converter=BooleanToIntegerConverter.class)
boolean fullTime;
...
}
Example 2: 基本属性の変換の自動適用
@Converter(autoApply=true)
public class EmployeeDateConverter
implements AttributeConverter<com.acme.EmployeeDate, java.sql.Date> { ... }
@Entity
public class Employee {
@Id long id;
...
// EmployeeDateConverter is applied automatically
EmployeeDate startDate;
}
Example 3: 自動適用コンバーターがある場合の変換の無効化
@Convert(disableConversion=true)
EmployeeDate lastReview;
Example 4: 基本型の要素コレクションへのコンバーターの適用
@ElementCollection
// applies to each element in the collection
@Convert(converter=NameConverter.class)
List<String> names;
Example 5: Mapもしくは基本値である要素コレクションへのコンバーターの適用。
コンバーターはMapの値に適用される。
@ElementCollection
@Convert(converter=EmployeeNameConverter.class)
Map<String, String> responsibilities;
Example 6: 基本型のMapのキーへのコンバーターの適用
@OneToMany
@Convert(converter=ResponsibilityCodeConverter.class,
attributeName="key")
Map<String, Employee> responsibilities;
Example 7: 組み込み属性へのコンバーターの適用
@Embedded
@Convert(converter=CountryConverter.class,
attributeName="country")
Address address;
Example 8: ネストした組み込み属性へのコンバーターの適用
@Embedded
@Convert(converter=CityConverter.class,
attributeName="region.city")
Address address;
Example 9: 要素コレクションのMapのキーとしてネストした組み込みクラスへのコンバーターの適用
@Entity public class PropertyRecord {
...
@Convert(attributeName="key.region.city",
converter=CityConverter.class)
@ElementCollection
Map<Address, PropertyInfo> parcels;
}
Example 10: 関連のマップのキーである組み込みクラスへのコンバーターの適用
@OneToMany
@Convert(attributeName="key.jobType",
converter=ResponsibilityTypeConverter.class)
Map<Responsibility, Employee> responsibilities;
Example 11: マップドクラスから継承した属性の変換マッピングの上書き
@Entity
@Converts({
@Convert(attributeName="startDate",
converter=DateConverter.class),
@Convert(attributeName="endDate",
converter=DateConverter.class)})
public class FullTimeEmployee extends GenericEmployee { ... }
| 修飾子とタイプ | 任意要素と説明 |
|---|---|
java.lang.String |
attributeName
Convertアノテーションが基本型や基本型の要素コレクションの属性に適用されている場合を除きattributeName要素は指定する必要があります。 |
java.lang.Class |
converter
適用するコンバーターを指定します。
|
boolean |
disableConversion
自動適用されるか継承されたコンバーターを無効化するのに使用します。
|
public abstract java.lang.Class converter
Translated by @megascus.