Use the <tt>@Enumerated</tt> annotation to specify whether EclipseLink persistence provider should persist ordinal or <tt>String</tt> values of enumerated constants if the <tt>String</tt> value suits your application requirements, or to match an existing database schema:

+

Use the <tt>@Enumerated</tt> annotation to specify whether EclipseLink persistence provider should persist ordinal (integer) or <tt>String</tt> values of enumerated constants if the <tt>String</tt> value suits your application requirements, or to match an existing database schema:

−

You can use this annotation with the [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings|@Basic annotation]].

+

You can use this annotation with the [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings|<code>@Basic</code>]] annotation.

{{EclipseLink_AttributeTable

{{EclipseLink_AttributeTable

−

|caption=The <tt>@Enumerated</tt> annotation has the following attributes:

+

|caption=@Enumerated Attributes

|content=<tr>

|content=<tr>

<td>'''<tt>value</tt>'''</td>

<td>'''<tt>value</tt>'''</td>

−

<td>By default, EclipseLink persistence provider assumes that for a property or field mapped to an enumerated constant, the ordinal value should be persisted. In the [[#Example 18-13|Usage of the @Enumerated Annotation]] example, the ordinal value of <tt>EmployeeStatus</tt> is written to the database when <tt>Employee</tt> is persisted.<br>If you want the <tt>String</tt> value of the enumerated constant persisted, set value to <tt>EnumType.STRING</tt>.</td>

+

<td>By default, EclipseLink persistence provider assumes that for a property or field mapped to an enumerated constant, the ordinal value should be persisted. In the [[#Example: @Enumerated Annotation|@Enumerated Annotation]] example, below, the ordinal value of <tt>EmployeeStatus</tt> is written to the database when <tt>Employee</tt> is persisted.<br>If you want the <tt>String</tt> value of the enumerated constant persisted, set value to <tt>EnumType.STRING</tt>.</td>

−

<td></td>

+

<td><code>ORDINAL</code></td>

<td>No</td>

<td>No</td>

</tr>

</tr>

}}

}}

−

Given the enumerated constants in the [[#Example 18-12|Enumerated Constants]] example, the [[#Example 18-13|Usage of the @Enumerated Annotation]] example shows how to use the <tt>@Enumerated</tt> annotation to specify that the <tt>String</tt> value of <tt>SalaryRate</tt> should be written to the database when <tt>Employee</tt> is persisted. By default, the ordinal value of <tt>EmployeeStatus</tt> is written to the database.

+

Given the enumerated constants in the [[#Example: Enumerated Constants|Enumerated Constants]] example, the [[#Example: @Enumerated Annotation|@Enumerated Annotation]] example, below, shows how to use the <tt>@Enumerated</tt> annotation to specify that the <tt>String</tt> value of <tt>SalaryRate</tt> should be written to the database when <tt>Employee</tt> is persisted. By default, the ordinal value of <tt>EmployeeStatus</tt> is written to the database.

Another option for mapping enumerated values is to use an <code>ObjectTypeConverter</code>. A converter allows for type codes to be used on the database instead of the enum's name, which may be required for legacy data.

By default, EclipseLink persistence provider assumes that for a property or field mapped to an enumerated constant, the ordinal value should be persisted. In the @Enumerated Annotation example, below, the ordinal value of EmployeeStatus is written to the database when Employee is persisted.If you want the String value of the enumerated constant persisted, set value to EnumType.STRING.

ORDINAL

No

Given the enumerated constants in the Enumerated Constants example, the @Enumerated Annotation example, below, shows how to use the @Enumerated annotation to specify that the String value of SalaryRate should be written to the database when Employee is persisted. By default, the ordinal value of EmployeeStatus is written to the database.

Example: Using <enumerated> XML

For more information, see Section 9.1.21 "Enumerated Annotation" in the JPA Specification.

Legacy Data

Another option for mapping enumerated values is to use an ObjectTypeConverter. A converter allows for type codes to be used on the database instead of the enum's name, which may be required for legacy data.