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


@ObjectTypeConverter

The @ObjectTypeConverter annotation specifies an org.eclipse.persistence.mappings.converters.ObjectTypeConverter that converts a fixed number of database data value(s) to Java object value(s) during the reading and writing of a mapped attribute.


Annotation Elements

Table 2-43 describes this annotation's elements.

Table 2-43 @ObjectTypeConverter Annotation Elements

Annotation Element Description Default

name

Set this attribute to the String name for your converter. Ensure that this name is unique across the persistence unit

none

dataType

(Optional) Set this attribute to the type stored in the database.

void.classFoot 

objectType

(Optional) Set the value of this attribute to the type stored on the entity.

void.classFootref 1

conversionValues

Set the value of this attribute to the array of conversion values (instances of ConversionValue: String objectValue and String dataValue).

none

defaultObjectValue

Set the value of this attribute to the default object value. Note that this argument is for dealing with legacy data if the data value is missing.

Empty String


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


Usage

EclipseLink also includes @TypeConverter and @StructConverter converters.


Examples

Example 2-80 shows how to use the @ObjectTypeConverter annotation to specify object converters for the gender field.

Example 2-80 Using the @ObjectTypeConverter Annotation

public class Employee implements Serializable{
     ...
     @ObjectTypeConverter (
         name="genderConverter",
         dataType=java.lang.String.class,
         objectType=java.lang.String.class,
         conversionValues={
             @ConversionValue(dataValue="F", objectValue="Female"),
             @ConversionValue(dataValue="M", objectValue="Male")}
     )
     @Convert("genderConverter")
     public String getGender() {
         return gender;
     }
     ...
 }

You can use the <object-type-converter> element in the deployment descriptor as an alternative to using the @ObjectTypeConverter annotation in the source code, as shown in Example 2-81.

Example 2-81 Using <object-type-converter> XML

<object-type-converter name="gender-converter" object-type="model.Gender"
data-type="java.lang.String">
<conversion-value object-value="Male" data-value="M" /> <conversion-value object-value="Female" data-value="F" /> </object-type-converter>


See Also

For more information, see: