Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Basic Mappings/Default Conversions and Converters/TypeConverter"

m
m (@TypeConverter Annotation)
Line 12: Line 12:
 
  }
 
  }
 
</source>
 
</source>
This table lists attributes of the <tt>@TypeConverter</tt> annotation.
+
{{EclipseLink_AttributeTable
 
+
|captioni=<span id="Table 19-8">@TypeConverter Attributes</span>
<span id="Table 19-8"></span>
+
|content=
{| class="RuleFormalWideMax" dir="ltr" title="Attributes of the @TypeConverter Annotation" summary="This table lists the attributes of EclipseLink JPA @TypeConverter annotation" width="100%" border="1" frame="border" rules="all" cellpadding="3" frame="border" rules="all"
+
<tr>
|- align="left" valign="top"
+
<td>'''<tt>name</tt>'''</td>
! id="r1c1-t13" align="left" valign="bottom" | '''Attribute'''
+
<td>Set this attribute to the <tt>String</tt> name for your converter. Ensure that this name is unique across the persistence unit.</td>
! id="r1c2-t13" align="left" valign="bottom" | '''Description'''
+
<td></td>
! id="r1c3-t13" align="left" valign="bottom" | '''Default'''
+
<td>Yes</td>
! id="r1c4-t13" align="left" valign="bottom" | '''Required or Optional'''
+
</tr>
|- align="left" valign="top"
+
<tr>
| id="r2c1-t13" headers="r1c1-t13" align="left" |
+
<td>'''<tt>dataType</tt>'''</td>
<tt>name</tt>
+
<td>Set this attribute to the type stored in the database.</td>
| headers="r2c1-t13 r1c2-t13" align="left" |
+
<td><tt>void.class</tt><sup>1</sup></td>
Set this attribute to the <tt>String</tt> name for your converter. Ensure that this name is unique across the persistence unit.
+
<td>No</td>
| headers="r2c1-t13 r1c3-t13" align="left" |
+
</tr>
no default
+
<tr>
| headers="r2c1-t13 r1c4-t13" align="left" |
+
<td>'''<tt>objectType</tt>'''</td>
required
+
<td>Set the value of this attribute to the type stored on the entity.</td>
|- align="left" valign="top"
+
<td><tt>void.class</tt><sup>1</sup></td>
| id="r3c1-t13" headers="r1c1-t13" align="left" |
+
<td>No</td>
<tt>dataType</tt>
+
</tr>
| headers="r3c1-t13 r1c2-t13" align="left" |
+
}}
Set this attribute to the type stored in the database.
+
<sup>1</sup> The default is inferred from the type of the persistence field or property.
| headers="r3c1-t13 r1c3-t13" align="left" |
+
<tt>void.class</tt><sup>1</sup>
+
| headers="r3c1-t13 r1c4-t13" align="left" |
+
optional
+
|- align="left" valign="top"
+
| id="r4c1-t13" headers="r1c1-t13" align="left" |
+
<tt>objectType</tt>
+
| headers="r4c1-t13 r1c2-t13" align="left" |
+
Set the value of this attribute to the type stored on the entity.
+
| headers="r4c1-t13 r1c3-t13" align="left" |
+
<tt>void.class</tt><sup>1</sup>
+
| headers="r4c1-t13 r1c4-t13" align="left" |
+
optional
+
|}
+
<br><sup>1</sup> The default is inferred from the type of the persistence field or property.
+
  
  

Revision as of 14:20, 17 June 2010


Elug api package icon.png Key API {{{apis}}}

@TypeConverter Annotation

The @TypeConverter is an EclipseLink-specific annotation. You can use it to specify an org.eclipse.persistence.mappings.converters.TypeConversionConverter for modification of the data value(s) during the reading and writing of a mapped attribute.

 
 @Target({TYPE, METHOD, FIELD})
 @Retention(RUNTIME)
 public @interface TypeConverter {
    String name();
    Class dataType() default void.class;
    Class objectType() default void.class;
 }
Attribute Description Default Required?
name Set this attribute to the String name for your converter. Ensure that this name is unique across the persistence unit. Yes
dataType Set this attribute to the type stored in the database. void.class1 No
objectType Set the value of this attribute to the type stored on the entity. void.class1 No

1 The default is inferred from the type of the persistence field or property.


This example shows how to use the @TypeConverter annotation to convert the Double value stored in the database to a Float value stored in the entity.

Usage of the @TypeConverter Annotation

 @Entity
 public class Employee implements Serializable{
     ...
     @TypeConverter (
         name="doubleToFloat",
         dataType=Double.class,
         objectType=Float.class,
     )
     @Convert("doubleToFloat")
     public Number getGradePointAverage() {
         return gradePointAverage;
     }
     ...
 }



Eclipselink-logo.gif
Version: 2.1.0
Other versions...