Jump to: navigation, search

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

m (New page: {{EclipseLink_API |1=*[http://www.eclipse.org/eclipselink/api/ org.eclipse.persistence.platform.database.DatabasePlatform] |2=*[http://www.eclipse.org/eclipselink/api/ org.eclipse.persiste...)
 
m
 
(17 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{EclipseLink_API
+
{{EclipseLink_UserGuide
|1=*[http://www.eclipse.org/eclipselink/api/ org.eclipse.persistence.platform.database.DatabasePlatform]
+
|info=y
|2=*[http://www.eclipse.org/eclipselink/api/ org.eclipse.persistence.database.platform.converters.StructConverter]
+
|toc=n
 +
|eclipselink=y
 +
|eclipselinktype=JPA
 +
|api=y
 +
|apis=
 +
*[http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/Convert.html @Convert]
 +
*[http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/mappings/converters/SerializedObjectConverter.html SerializedObjectConverter]
 
}}
 
}}
=@Convert Annotation=
+
 
 +
=@Convert=
 +
The <tt>@Convert</tt> annotation specifies that a named converter should be used with the corresponding mapped attribute.
 +
<source lang="java">
 +
@Target({METHOD, FIELD})
 +
@Retention(RUNTIME)
 +
public @interface Convert {
 +
    String value() default "none";
 +
}
 +
</source>
 +
The <tt>@Convert</tt> has the following reserved names:
 +
 
 +
* <tt>serialized</tt> – places the <tt>org.eclipse.persistence.mappings.converters.SerializedObjectConverter</tt> on the associated mapping.
 +
* <tt>none</tt> – does not place a converter on the associated mapping.
 +
 
 +
{{EclipseLink_AttributeTable
 +
|caption=@Convert Annotation Attributes
 +
|content=<tr>
 +
<td>'''<tt>value</tt>'''</td>
 +
<td>The <tt>String</tt> name for your converter.</td>
 +
<td><tt>"none" String</tt></td>
 +
<td>No</td>
 +
</tr>
 +
}}
 +
 
 +
 
 +
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>
 +
======''Example: @Convert Annotation''======
 +
<source lang="java">
 +
@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;
 +
    }
 +
    ...
 +
}
 +
</source>
 +
 
 +
 
 +
 
  
 
{{EclipseLink_JPA
 
{{EclipseLink_JPA
 
|previous=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters/StructConverter|@StructConverter]]
 
|previous=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters/StructConverter|@StructConverter]]
|next=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Lazy_Basics|Lazy Basics]]
+
|next=   [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Lazy_Basics|Lazy Basics]]
|version=2.1.0
+
|up=      [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters|Default Conversions and Converters]]
}}
+
|version=2.2.0 DRAFT}}

Latest revision as of 09:09, 5 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


@Convert

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...