EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Basic Mappings/Default Conversions and Converters/ObjectTypeConverter

From Eclipsepedia

< EclipseLink‎ | UserGuide‎ | JPA‎ | Basic JPA Development‎ | Mapping‎ | Basic Mappings‎ | Default Conversions and Converters
Revision as of 09:56, 17 June 2010 by Rick.sapir.oracle.com (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

{EclipseLink_API |1=org.eclipse.persistence.mappings.converters.ObjectTypeConverter }}

@ObjectTypeConverter Annotation

You can use the @ObjectTypeConverter annotation to specify an org.eclipse.persistence.mappings.converters.ObjectTypeConverter that converts a fixed number of database data value(s) to Java object value(s) during the reading and writing of a mapped attribute.

 @Target({TYPE, METHOD, FIELD})
 @Retention(RUNTIME)
 public @interface ObjectTypeConverter {
    String name();
    Class dataType() default void.class;
    Class objectType() default void.class;
    ConversionValue[] conversionValues();
    String defaultObjectValue() default "";
 }
</java>
This table lists attributes of the <tt>@ObjectTypeConverter</tt> annotation.
 
<span id="Table 19-9"></span>
{| class="RuleFormalWideMax" dir="ltr" title="Attributes of the @ObjectTypeConverter Annotation" summary="This table lists the attributes of EclipseLink JPA @ObjectTypeConverter annotation" width="100%" border="1" frame="border" rules="all" cellpadding="3" frame="border" rules="all"
|- align="left" valign="top"
! id="r1c1-t14" align="left" valign="bottom" | '''Attribute'''
! id="r1c2-t14" align="left" valign="bottom" | '''Description'''
! id="r1c3-t14" align="left" valign="bottom" | '''Default'''
! id="r1c4-t14" align="left" valign="bottom" | '''Required or Optional'''
|- align="left" valign="top"
| id="r2c1-t14" headers="r1c1-t14" align="left" |
<tt>name</tt>
| headers="r2c1-t14 r1c2-t14" align="left" |
Set this attribute to the <tt>String</tt> name for your converter. Ensure that this name is unique across the persistence unit
| headers="r2c1-t14 r1c3-t14" align="left" |
no default
| headers="r2c1-t14 r1c4-t14" align="left" |
required
|- align="left" valign="top"
| id="r3c1-t14" headers="r1c1-t14" align="left" |
<tt>dataType</tt>
| headers="r3c1-t14 r1c2-t14" align="left" |
Set this attribute to the type stored in the database.
| headers="r3c1-t14 r1c3-t14" align="left" |
<tt>void.class</tt><sup>1</sup>
| headers="r3c1-t14 r1c4-t14" align="left" |
optional
|- align="left" valign="top"
| id="r4c1-t14" headers="r1c1-t14" align="left" |
<tt>objectType</tt>
| headers="r4c1-t14 r1c2-t14" align="left" |
Set the value of this attribute to the type stored on the entity.
| headers="r4c1-t14 r1c3-t14" align="left" |
<tt>void.class</tt><sup>1</sup>
| headers="r4c1-t14 r1c4-t14" align="left" |
optional
|- align="left" valign="top"
| id="r5c1-t14" headers="r1c1-t14" align="left" |
<tt>conversionValues</tt>
| headers="r5c1-t14 r1c2-t14" align="left" |
Set the value of this attribute to the array of conversion values (instances of <tt>ConversionValue</tt><nowiki>: </nowiki><tt>String objectValue</tt> and <tt>String dataValue</tt>. See the [[#Example 19-9| Usage of the @ObjectTypeConverter Annotation]] example, to be used with the object converter.
| headers="r5c1-t14 r1c3-t14" align="left" |
no default
| headers="r5c1-t14 r1c4-t14" align="left" |
required
|- align="left" valign="top"
| id="r6c1-t14" headers="r1c1-t14" align="left" |
<tt>defaultObjectValue</tt>
| headers="r6c1-t14 r1c2-t14" align="left" |
Set the value of this attribute to the default object value. Note that this argument is for dealing with legacy data if the data value is missing.
| headers="r6c1-t14 r1c3-t14" align="left" |
empty <tt>String</tt>
| headers="r6c1-t14 r1c4-t14" align="left" |
optional
|}
<br><sup>1</sup> The default is inferred from the type of the persistence field or property.
 
This example shows how to use the <tt>@ObjectTypeConverter</tt> annotation to specify the <tt>Employee</tt> field <tt>gender</tt>.
 
<span id="Example 19-9"></span>
''''' Usage of the @ObjectTypeConverter Annotation'''''
<source lang="java">
 public class Employee implements Serializable{
     ...
     @ObjectTypeConverter (
         name="genderConverter",
         dataType=java.lang.String.class,
         objectType=java.lang.String.class,
         conversionValues={
             @ConversionValue(dataValue="F", objectValue="Female"),
             @ConversionValue(dataValue="M", objectValue="Male")}
     )
     @Convert("genderConverter")
     public String getGender() {
         return gender;
     }
     ...
 }