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

From Eclipsepedia

Jump to: navigation, search
m
Line 9: Line 9:
 
}}
 
}}
  
=@Convert Annotation and <convert> XML =
+
=@Convert Annotation=
 
The <tt>@Convert</tt> annotation specifies that a named converter should be used with the corresponding mapped attribute.
 
The <tt>@Convert</tt> annotation specifies that a named converter should be used with the corresponding mapped attribute.
 
<source lang="java">
 
<source lang="java">
Line 24: Line 24:
  
 
{{EclipseLink_AttributeTable
 
{{EclipseLink_AttributeTable
|caption=Attributes of the <tt>@Convert</tt> annotation
+
|caption=@Convert Annotation Attributes
 
|content=<tr>
 
|content=<tr>
 
  <td>'''<tt>value</tt>'''</td>
 
  <td>'''<tt>value</tt>'''</td>
Line 34: Line 34:
  
  
This example shows how to use the <tt>@Convert</tt> annotation to define the <tt>Employee</tt> field <tt>gender</tt>.
+
The following example shows how to use the <tt>@Convert</tt> annotation to define the <tt>Employee</tt> field <tt>gender</tt>.
  
 
<span id="Example 19-11"></span>
 
<span id="Example 19-11"></span>
''''' Usage of the @Convert Annotation'''''
+
======''Example: @Convert Annotation''======
 
<source lang="java">
 
<source lang="java">
 
  @Entity
 
  @Entity

Revision as of 14:47, 29 March 2011

EclipseLink JPA

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


@Convert Annotation

The @Convert annotation specifies that a named converter should be used with the corresponding mapped attribute.

 @Target({METHOD, FIELD})
 @Retention(RUNTIME)
 public @interface Convert {
    String value() default "none";
 }

The @Convert has the following reserved names:

  • serialized – places the org.eclipse.persistence.mappings.converters.SerializedObjectConverter on the associated mapping.
  • none – does not place a converter on the associated mapping.
@Convert Annotation Attributes
Attribute Description Default Required?
value The String name for your converter. "none" String No


The following example shows how to use the @Convert annotation to define the Employee field gender.

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



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