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

m (@Converter)
m (@Converter)
Line 11: Line 11:
 
  }
 
  }
 
</source>
 
</source>
This table lists attributes of the <tt>@Converter</tt> annotation.
+
{{EclipseLink_AttributeTable
 
+
|caption=<span id="Table 19-7">@Converter Attributes</span>
<span id="Table 19-7"></span>
+
|content=<tr>
{| class="RuleFormalWideMax" dir="ltr" title="Attributes of the @Converter Annotation" summary="This table lists the attributes of EclipseLink JPA @Converter annotation" width="100%" border="1" frame="border" rules="all" cellpadding="3" frame="border" rules="all"
+
<td>'''<tt>name</tt>'''</td>
|- align="left" valign="top"
+
<td>Set this attribute to the <tt>String</tt> name for your converter. Ensure that this name is unique across the persistence unit</td>
! id="r1c1-t12" align="left" valign="bottom" | '''Attribute'''
+
<td></td>
! id="r1c2-t12" align="left" valign="bottom" | '''Description'''
+
<td>Yes</td>
! id="r1c3-t12" align="left" valign="bottom" | '''Default'''
+
</tr>
! id="r1c4-t12" align="left" valign="bottom" | '''Required or Optional'''
+
<tr>
|- align="left" valign="top"
+
<td>'''<tt>converterClass</tt>'''</td>
| id="r2c1-t12" headers="r1c1-t12" align="left" |
+
<td>Set this attribute to the <tt>Class</tt> of your converter. This class must implement the EclipseLink <tt>org.eclipse.persistence.mappings.converters.Converter</tt> interface.</td>
<tt>name</tt>
+
<td></td>
| headers="r2c1-t12 r1c2-t12" align="left" |
+
<td>Yes</td>
Set this attribute to the <tt>String</tt> name for your converter. Ensure that this name is unique across the persistence unit
+
</tr>
| headers="r2c1-t12 r1c3-t12" align="left" |
+
}}
no default
+
| headers="r2c1-t12 r1c4-t12" align="left" |
+
required
+
|- align="left" valign="top"
+
| id="r3c1-t12" headers="r1c1-t12" align="left" |
+
<tt>converterClass</tt>
+
| headers="r3c1-t12 r1c2-t12" align="left" |
+
Set this attribute to the <tt>Class</tt> of your converter. This class must implement the EclipseLink <tt>org.eclipse.persistence.mappings.converters.Converter</tt> interface.
+
| headers="r3c1-t12 r1c3-t12" align="left" |
+
no default
+
| headers="r3c1-t12 r1c4-t12" align="left" |
+
required
+
|}
+
 
+
  
 
This example shows how to use the <tt>@Converter</tt> annotation to specify <tt>Employee</tt> field <tt>gender</tt>.
 
This example shows how to use the <tt>@Converter</tt> annotation to specify <tt>Employee</tt> field <tt>gender</tt>.

Revision as of 14:16, 17 June 2010

@Converter

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

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

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

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

Copyright © Eclipse Foundation, Inc. All Rights Reserved.