Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Basic Mappings/Default Conversions and Converters/TypeConverter"
< EclipseLink | UserGuide | JPA | Basic JPA Development | Mapping | Basic Mappings | Default Conversions and Converters
m |
m |
||
(4 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
|info=y | |info=y | ||
|api=y | |api=y | ||
− | |apis=[http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/TypeConverter.html TypeConverter] | + | |apis= |
+ | *[http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/TypeConverter.html @TypeConverter] | ||
}} | }} | ||
− | =@TypeConverter | + | =@TypeConverter= |
− | + | <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. | |
<source lang="java"> | <source lang="java"> | ||
@Target({TYPE, METHOD, FIELD}) | @Target({TYPE, METHOD, FIELD}) | ||
Line 16: | Line 17: | ||
</source> | </source> | ||
{{EclipseLink_AttributeTable | {{EclipseLink_AttributeTable | ||
− | |caption=<span>@TypeConverter Attributes</span> | + | |caption=<span>@TypeConverter Annotation Attributes</span> |
|content= | |content= | ||
<tr> | <tr> | ||
Line 40: | Line 41: | ||
− | + | The following example shows how to use the <tt>@TypeConverter</tt> annotation to convert the <tt>Double</tt> value stored in the database to a <tt>Float</tt> value stored in the entity. | |
<span id="Example 19-8"></span> | <span id="Example 19-8"></span> | ||
− | '' | + | ======'' Example: @TypeConverter Annotation''====== |
<source lang="java"> | <source lang="java"> | ||
@Entity | @Entity |
Latest revision as of 10:04, 5 May 2011
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
Key API
@TypeConverter
@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.
The following 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.
Example: @TypeConverter Annotation
@Entity public class Employee implements Serializable{ ... @TypeConverter ( name="doubleToFloat", dataType=Double.class, objectType=Float.class, ) @Convert("doubleToFloat") public Number getGradePointAverage() { return gradePointAverage; } ... }