Jump to: navigation, search

EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Basic Mappings/Default Conversions and Converters/ObjectTypeConverter

< EclipseLink‎ | UserGuide‎ | JPA‎ | Basic JPA Development‎ | Mapping‎ | Basic Mappings‎ | Default Conversions and Converters
Revision as of 10:05, 5 May 2011 by Ben.gelernter.oracle.com (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Mailing ListForumsIRCmattermost
OpenHelp WantedBug Day
Browse Source


You can use the @ObjectTypeConverter annotation to specify 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.

 public @interface ObjectTypeConverter {
    String name();
    Class dataType() default void.class;
    Class objectType() default void.class;
    ConversionValue[] conversionValues();
    String defaultObjectValue() default "";
@ObjectTypeConverter Annotation Attributes
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 no default required
dataType Set this attribute to the type stored in the database. void.class1 optional
objectType Set the value of this attribute to the type stored on the entity. void.class1 optional
conversionValues Set the value of this attribute to the array of conversion values (instances of ConversionValue: String objectValue and String dataValue. See the @ObjectTypeConverter Annotation example, to be used with the object converter. no default required
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 optional

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

The following example shows how to use the @ObjectTypeConverter annotation to specify the Employee field gender.

Example: @ObjectTypeConverter Annotation
 public class Employee implements Serializable{
     @ObjectTypeConverter (
             @ConversionValue(dataValue="F", objectValue="Female"),
             @ConversionValue(dataValue="M", objectValue="Male")}
     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 the following example:

Example: <object-type-converter> Element
  <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" />

Version: 2.2.0 DRAFT
Other versions...