Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Basic Mappings/Default Conversions and Converters/TypeConverter"
m (EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Basic Mappings/Default Conversions and Converters/Converter/TypeConverter moved to [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Basic Mappings/Default Conversions and Converters/Type) |
m |
||
Line 1: | Line 1: | ||
{{EclipseLink_API | {{EclipseLink_API | ||
− | |1=*org.eclipse.persistence.mappings.converters.TypeConversionConverter}} | + | |1=*[http://www.eclipse.org/eclipselink/api/ org.eclipse.persistence.mappings.converters.TypeConversionConverter]}} |
=@TypeConverter Annotation= | =@TypeConverter Annotation= | ||
The <tt>@TypeConverter</tt> is an EclipseLink-specific annotation. You can use it to specify an <tt>org.eclipse.persistence.mappings.converters.TypeConversionConverter</tt> for modification of the data value(s) during the reading and writing of a mapped attribute. | The <tt>@TypeConverter</tt> is an EclipseLink-specific annotation. You can use it to specify an <tt>org.eclipse.persistence.mappings.converters.TypeConversionConverter</tt> for modification of the data value(s) during the reading and writing of a mapped attribute. | ||
Line 78: | Line 78: | ||
{{EclipseLink_JPA | {{EclipseLink_JPA | ||
|previous=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters/Converter|@Converter]] | |previous=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters/Converter|@Converter]] | ||
− | |next=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters | + | |next=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters/ObjectTypeConverter|@ObjectTypeConveter]] |
|up=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters|Default Conversions and Converters]] | |up=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters|Default Conversions and Converters]] | ||
|version=2.1.0 | |version=2.1.0 | ||
}} | }} |
Revision as of 10:04, 17 June 2010
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; }
This table lists attributes of the @TypeConverter annotation.
Attribute | Description | Default | Required or Optional |
---|---|---|---|
name |
Set this attribute to the String name for your converter. Ensure that this name is unique across the persistence unit. |
no default |
required |
dataType |
Set this attribute to the type stored in the database. |
void.class1 |
optional |
objectType |
Set the value of this attribute to the type stored on the entity. |
void.class1 |
optional |
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; } ... }