Use the @Converter
annotation to specify a custom converter for modification of the data value(s) during the reading and writing of a mapped attribute.
Table 2-15 describes this annotation's elements.
Table 2-15 @Converter Annotation Elements
Annotation Element | Description | Default |
---|---|---|
|
The |
none |
|
The class of your converter. This class must implement the |
none |
Use @Converter
to define a named converter that can be used with mappings. A converter can be defined on an entity class, method, or field. Specify a converter with the @Convert annotation on a Basic or ElementCollection mapping.
Using non-JPA Converter Annotations
EclipseLink provides a set of non-JPA converter annotations (in addition to the JPA default type mappings):
The persistence provider searches the converter annotations in the following order:
@Convert
@Enumerated
@Lob
@Temporal
Serialized (automatic)
Specify the converters on the following classes:
@Entity
@MappedSuperclass
@Embeddable
Use the converters with the following mappings:
@Basic
@Id
@Version
@ElementCollection
An exception is thrown if a converter is specified with any other type of mapping annotation.
Example 2-32 shows how to use the @Converter
annotation to specify a converter class for the gender
field.
Example 2-32 Using 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;
}
...
}
Example 2-33 shows how to use the <converter>
element in the eclipselink-orm.xml
file.
Example 2-33 Using <converter> XML
<entity class="Employee">
...
<attributes>
...
<basic name="gender">
<convert>genderConverter</convert>
<converter name="genderConverter" class="org.myorg.converters.GenderConverter"/>
</basic>
...
</attributes>
</entity>
For more information, see:
Understanding Oracle TopLink