Java Persistence API (JPA) Extensions Reference for EclipseLink, Release 2.4
  Go To Table Of Contents
 Search
 PDFComments
Comments


@Converter

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.


Annotation Elements

Table 2-15 describes this annotation's elements.

Table 2-15 @Converter Annotation Elements

Annotation Element Description Default

name

The String name for your converter, must be unique across the persistence unit

none

converterClass

The class of your converter. This class must implement the org.eclipse.persistence.mappings.converters.Converter interface.

none



Usage

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:

  1. @Convert

  2. @Enumerated

  3. @Lob

  4. @Temporal

  5. Serialized (automatic)

Specify the converters on the following classes:

Use the converters with the following mappings:

An exception is thrown if a converter is specified with any other type of mapping annotation.


Examples

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>


See Also

For more information, see: