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


@ConversionValue

Use @ConversionValue to specify the database and object values for an ObjectTypeConverter.


Annotation Elements

Table 2-13 describes this annotation's elements.

Table 2-13 @ConversionValue Annotation Elements

Annotation Element Description Default

dataValue

(Required) The database value.


objectValue

(Required) The object value




Usage

The JPA specification allows you to map an Enum to database columns using the @Enumerated annotation, when the database value is either the name of the Enum or its ordinal value. With EclipseLink, you can also map an Enum to a coded value, using a converter.


Examples

In Example 2-29, the enum Gender(MALE, FEMALE) is mapped to a single character in the database where M=MALE and F=FEMALE.

Example 2-29 Using @ConversionValue Annotation

@ObjectTypeConverter(name = "gender", objectType = Gender.class, dataType = String.class, conversionValues = {
  @ConversionValue(objectValue = "Male", dataValue = "M"),
  @ConversionValue(objectValue = "Female", dataValue = "F") })

...

@Basic
@Convert("gender")
private Gender gender = Gender.Male;

Example 2-30 illustrates the same function using XML.

Example 2-30 Using <conversion-value> XML

<object-type-converter name="gender" 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>

...

<basic name="gender">
  <column name="GENDER" />
  <convert>gender</convert>
</basic>


See Also

For more information, see: