Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

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

m (New page: {{EclipseLink_API |1=*org.eclipse.persistence.mappings.converters.TypeConversionConverter}} =@TypeConverter Annotation= The <tt>@TypeConverter</tt> is an EclipseLink-specific annotation. Y...)
 
m
 
(14 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{EclipseLink_API
+
{{EclipseLink_UserGuide
|1=*org.eclipse.persistence.mappings.converters.TypeConversionConverter}}
+
|info=y
=@TypeConverter Annotation=
+
|api=y
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.
+
|apis=
 +
*[http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/TypeConverter.html @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 12: Line 16:
 
  }
 
  }
 
</source>
 
</source>
This table lists attributes of the <tt>@TypeConverter</tt> annotation.
+
{{EclipseLink_AttributeTable
 +
|caption=<span>@TypeConverter Annotation Attributes</span>
 +
|content=
 +
<tr>
 +
<td>'''<tt>name</tt>'''</td>
 +
<td>Set this attribute to the <tt>String</tt> name for your converter. Ensure that this name is unique across the persistence unit.</td>
 +
<td></td>
 +
<td>Yes</td>
 +
</tr>
 +
<tr>
 +
<td>'''<tt>dataType</tt>'''</td>
 +
<td>Set this attribute to the type stored in the database.</td>
 +
<td><tt>void.class</tt><sup>1</sup></td>
 +
<td>No</td>
 +
</tr>
 +
<tr>
 +
<td>'''<tt>objectType</tt>'''</td>
 +
<td>Set the value of this attribute to the type stored on the entity.</td>
 +
<td><tt>void.class</tt><sup>1</sup></td>
 +
<td>No</td>
 +
</tr>
 +
}}
 +
<sup>1</sup> The default is inferred from the type of the persistence field or property.
  
<span id="Table 19-8"></span>
 
{| 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"
 
|- align="left" valign="top"
 
! id="r1c1-t13" align="left" valign="bottom" | '''Attribute'''
 
! id="r1c2-t13" align="left" valign="bottom" | '''Description'''
 
! id="r1c3-t13" align="left" valign="bottom" | '''Default'''
 
! id="r1c4-t13" align="left" valign="bottom" | '''Required or Optional'''
 
|- align="left" valign="top"
 
| id="r2c1-t13" headers="r1c1-t13" align="left" |
 
<tt>name</tt>
 
| headers="r2c1-t13 r1c2-t13" 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-t13 r1c3-t13" align="left" |
 
no default
 
| headers="r2c1-t13 r1c4-t13" align="left" |
 
required
 
|- align="left" valign="top"
 
| id="r3c1-t13" headers="r1c1-t13" align="left" |
 
<tt>dataType</tt>
 
| headers="r3c1-t13 r1c2-t13" align="left" |
 
Set this attribute to the type stored in the database.
 
| 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.
 
  
 
+
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.
This 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>
''''' Usage of the @TypeConverter Annotation'''''
+
======'' Example: @TypeConverter Annotation''======
 
<source lang="java">
 
<source lang="java">
 
  @Entity
 
  @Entity
Line 78: Line 67:
 
{{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/Converter/ObjectTypeConverter|@ObjectTypeConveter]]
+
|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
 
}}
 
}}

Latest revision as of 10:04, 5 May 2011


Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug Day
Contribute
Browse Source

Elug api package icon.png 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;
 }
@TypeConverter Annotation Attributes
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;
     }
     ...
 }



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

Copyright © Eclipse Foundation, Inc. All Rights Reserved.