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


@TypeConverter

Use @TypeConverter to modify data values during the reading and writing of a mapped attribute.


Annotation Elements

Table 2-72 describes this annotation's elements.

Table 2-72 @TypeConverter Annotation Elements

Annotation Element Description Default

name

(Required) The String name for your converter. This name must be unique across the persistence unit.

none

dataType

(Optional) The type stored in the database.

void.classFoot 

objectType

(Optional) The type stored on the entity.

void.classFootref 1


Footnote The default is inferred from the type of the persistence field or property.


Usage

Each TypeConverter must be uniquely named and can be defined at the class, field and property level and can be specified within an Entity, MappedSuperclass and Embeddable class. A TypeConverter is always specified by using an @Convert annotation

You can place a @TypeConverter on a Basic, BasicMap or BasicCollection mapping.

EclipseLink also includes @ObjectTypeConverter and @StructConverter converters.


Examples

Example 2-116 shows how to use the @TypeConverter annotation to convert the Double value stored in the database to a Float value stored in the entity.

Example 2-116 Using the @TypeConverter Annotation

@Entity
public class Employee implements Serializable{

...

  @TypeConverter (
    name="doubleToFloat",
    dataType=Double.class,
    objectType=Float.class,
  )
  @Convert("doubleToFloat")
  public Number getGradePointAverage() {
    return gradePointAverage;
  }

...
}


See Also

For more information, see: