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/Convert"

m
m
 
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{EclipseLink_UserGuide}}
+
{{EclipseLink_UserGuide
<!--{{EclipseLink_API
+
|info=y
|1=*[http://www.eclipse.org/eclipselink/api/ org.eclipse.persistence.mappings.converters.SerializedObjectConverter]
+
|toc=n
}}-->
+
|eclipselink=y
=@Convert Annotation and <convert> XML =
+
|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=
 
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 17: Line 24:
 
* <tt>none</tt> – does not place a converter on the associated mapping.
 
* <tt>none</tt> – does not place a converter on the associated mapping.
  
This table lists attributes of the <tt>@Convert</tt> annotation.
+
{{EclipseLink_AttributeTable
 
+
|caption=@Convert Annotation Attributes
<span id="Table 19-11"></span>
+
|content=<tr>
''''' Attributes of the @Convert Annotation'''''
+
<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>
 +
}}
  
{| class="RuleFormalWideMax" dir="ltr" title="Attributes of the @Convert Annotation" summary="This table lists the attributes of EclipseLink JPA @Convert annotation" width="100%" border="1" frame="border" rules="all" cellpadding="3" frame="border" rules="all"
 
|- align="left" valign="top"
 
! id="r1c1-t17" align="left" valign="bottom" | '''Attribute'''
 
! id="r1c2-t17" align="left" valign="bottom" | '''Description'''
 
! id="r1c3-t17" align="left" valign="bottom" | '''Default'''
 
! id="r1c4-t17" align="left" valign="bottom" | '''Required or Optional'''
 
|- align="left" valign="top"
 
| id="r2c1-t17" headers="r1c1-t17" align="left" |
 
<tt>value</tt>
 
| headers="r2c1-t17 r1c2-t17" align="left" |
 
Set this attribute to the <tt>String</tt> name for your converter.
 
| headers="r2c1-t17 r1c3-t17" align="left" |
 
<tt>"none" String</tt>
 
| headers="r2c1-t17 r1c4-t17" align="left" |
 
optional
 
|}
 
  
 
+
The following example shows how to use the <tt>@Convert</tt> annotation to define the <tt>Employee</tt> field <tt>gender</tt>.
This 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
Line 66: Line 61:
  
 
{{EclipseLink_JPA
 
{{EclipseLink_JPA
|previous=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters/StructConverter|Struct Converter]]
+
|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]]
 
|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 10:09, 5 May 2011

EclipseLink JPA

Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug 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...

Back to the top