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 (@Convert Annotation)
Line 4: Line 4:
 
}}
 
}}
 
=@Convert Annotation=
 
=@Convert Annotation=
 +
 +
 +
===How to Use the @Convert Annotation===
 +
 +
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.
 +
 +
This table lists attributes of the <tt>@Convert</tt> annotation.
 +
 +
<span id="Table 19-11"></span>
 +
''''' Attributes of the @Convert Annotation'''''
 +
 +
{| 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
 +
|}
 +
 +
 +
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>
 +
''''' Usage of the @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

Revision as of 10:08, 17 June 2010


Elug api package icon.png Key API {{{apis}}}

@Convert Annotation

How to Use the @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.

This table lists attributes of the @Convert annotation.

Attributes of the @Convert Annotation

Attribute Description Default Required or Optional

value

Set this attribute to the String name for your converter.

"none" String

optional


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

Usage of the @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.1.0
Other versions...