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.
Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Basic Mappings/Default Conversions and Converters/Converter"
< EclipseLink | UserGuide | JPA | Basic JPA Development | Mapping | Basic Mappings | Default Conversions and Converters
m |
m |
||
(2 intermediate revisions by the same user not shown) | |||
Line 6: | Line 6: | ||
|api=y | |api=y | ||
|apis= | |apis= | ||
− | * [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/Converter.html Converter]}} | + | * [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/Converter.html @Converter]}} |
− | =@Converter | + | =@Converter= |
You can use <tt>@Converter</tt> annotation to specify a custom converter for modification of the data value(s) during the reading and writing of a mapped attribute. | You can use <tt>@Converter</tt> annotation to specify a custom converter for modification of the data value(s) during the reading and writing of a mapped attribute. | ||
<source lang="java"> | <source lang="java"> | ||
Line 20: | Line 20: | ||
{{EclipseLink_AttributeTable | {{EclipseLink_AttributeTable | ||
− | |caption=<span id=" | + | |caption=<span id="@Converter Attributes">@Converter Annotation Attributes</span> |
|content=<tr> | |content=<tr> | ||
<td>'''<tt>name</tt>'''</td> | <td>'''<tt>name</tt>'''</td> | ||
Line 35: | Line 35: | ||
}} | }} | ||
− | |||
− | <span id="Example | + | The following example shows how to use the <tt>@Converter</tt> annotation to specify <tt>Employee</tt> field <tt>gender</tt>. |
− | '' | + | |
+ | <span id="Example: @Converter Annotation"></span> | ||
+ | ======''Example: @Converter Annotation''====== | ||
<source lang="java"> | <source lang="java"> | ||
@Entity | @Entity |
Latest revision as of 10:35, 4 May 2011
EclipseLink JPA
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
Key API
@Converter
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(); }
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 |
The following example shows how to use the @Converter annotation to specify Employee field gender.
Example: @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; } ... }