Jump to: navigation, search

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

m
m
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{EclipseLink_UserGuide|info=y|api=y
+
{{EclipseLink_UserGuide
|1=*[http://www.eclipse.org/eclipselink/api/latest org.eclipse.persistence.mappings.converters.Converter]}}
+
|info=y
=@Converter and <converter>=
+
|toc=n
 +
|eclipselink=y
 +
|eclipselinktype=JPA
 +
|api=y
 +
|apis=
 +
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/Converter.html @Converter]}}
 +
 
 +
=@Converter=
 
You can use <tt>@Converter</tt> annotation to specify a custom converter for modification of the data value(s) during the reading and writing of a mapped attribute.
 
You can use <tt>@Converter</tt> annotation to specify a custom converter for modification of the data value(s) during the reading and writing of a mapped attribute.
 
<source lang="java">  
 
<source lang="java">  
Line 11: Line 18:
 
  }
 
  }
 
</source>
 
</source>
 +
 
{{EclipseLink_AttributeTable
 
{{EclipseLink_AttributeTable
|caption=<span id="Table 19-7">@Converter Attributes</span>
+
|caption=<span id="@Converter Attributes">@Converter Annotation Attributes</span>
 
|content=<tr>
 
|content=<tr>
 
  <td>'''<tt>name</tt>'''</td>
 
  <td>'''<tt>name</tt>'''</td>
Line 27: Line 35:
 
}}
 
}}
  
This example shows how to use the <tt>@Converter</tt> annotation to specify <tt>Employee</tt> field <tt>gender</tt>.
 
  
<span id="Example 19-7"></span>
+
The following example shows how to use the <tt>@Converter</tt> annotation to specify <tt>Employee</tt> field <tt>gender</tt>.
''''' Usage of the @Converter Annotation'''''
+
 
 +
<span id="Example: @Converter Annotation"></span>
 +
======''Example: @Converter Annotation''======
 
<source lang="java">
 
<source lang="java">
 
  @Entity
 
  @Entity
Line 54: Line 63:
 
|next=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters/TypeConverter|@TypeConverter]]
 
|next=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters/TypeConverter|@TypeConverter]]
 
|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.2.0 DRAFT
 
}}
 
}}

Latest revision as of 09:35, 4 May 2011

EclipseLink JPA

link="http://wiki.eclipse.org/EclipseLink"
EclipseLink
Website
Download
Community
Mailing ListForumsIRC
Bugzilla
Open
Help Wanted
Bug Day
Contribute
Browse Source

Elug api package icon.png Key API


@Converter

You can use @Converter annotation to specify a custom converter for modification of the data value(s) during the reading and writing of a mapped attribute.

 
 @Target({TYPE, METHOD, FIELD})
 @Retention(RUNTIME)
 public @interface Converter {
    String name();
    Class converterClass(); 
 }
@Converter 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
converterClass Set this attribute to the Class of your converter. This class must implement the EclipseLink org.eclipse.persistence.mappings.converters.Converter interface. Yes


The following example shows how to use the @Converter annotation to specify Employee field gender.

Example: @Converter Annotation
 @Entity
 public class Employee implements Serializable{
     ...
     @Basic
     @Converter (
         name="genderConverter",
         converterClass=org.myorg.converters.GenderConverter.class
     )
     @Convert("genderConverter")
     public String getGender() {
         return gender;
     }
     ...
 }


Eclipselink-logo.gif
Version: 2.2.0 DRAFT
Other versions...