Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Basic Mappings/Enumerated"

m
(Example: Using XML)
 
(9 intermediate revisions by 2 users not shown)
Line 7: Line 7:
 
|apis=
 
|apis=
 
* [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/Enumerated.html @Enumerated]
 
* [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/Enumerated.html @Enumerated]
 +
* [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/EnumType.html EnumType]
 +
|examples=y
 +
|example=
 +
*[[EclipseLink/Examples/JPA/EnumToCode|Map Enum to Coded Values]]
 
}}
 
}}
  
Line 12: Line 16:
 
By default, EclipseLink persistence provider persists the ordinal values of enumerated constants.
 
By default, EclipseLink persistence provider persists the ordinal values of enumerated constants.
  
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
Line 20: Line 24:
 
|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>
Line 27: Line 31:
  
  
Given the enumerated constants in the [[#Example 18-12|Enumerated Constants]] example, the [[#Example 18-13|@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.
+
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.
  
 
======'' Example: Enumerated Constants''======
 
======'' Example: Enumerated Constants''======
 
<source lang="java">
 
<source lang="java">
public enum EmployeeStatus {FULL_TIME, PART_TIME, CONTRACT}
+
public enum EmployeeStatus {FULL_TIME, PART_TIME, CONTRACT}
public enum SalaryRate {JUNIOR, SENIOR, MANAGER, EXECUTIVE}
+
public enum SalaryRate {JUNIOR, SENIOR, MANAGER, EXECUTIVE}
 
</source>
 
</source>
  
 
======'' Example: @Enumerated Annotation''======
 
======'' Example: @Enumerated Annotation''======
 
<source lang="java">
 
<source lang="java">
@Entity
+
@Entity
public class Employee implements Serializable{
+
public class Employee implements Serializable{
    ...
+
    ...
    public EmployeeStatus getStatus() {
+
    public EmployeeStatus getStatus() {
        ...
+
        ...
    }
+
    }
+
 
    @Enumerated(STRING)
+
    @Enumerated(STRING)
    public SalaryRate getRate() {
+
    public SalaryRate getRate() {
        ...
+
        ...
    }
+
    }
    ...
+
    ...
}
+
}
 +
</source>
 +
 
 +
======''Example: Using <code><nowiki><enumerated></nowiki></code> XML''======
 +
 
 +
<source lang="xml">
 +
<entity class="Employee">
 +
    <attributes>
 +
        ...
 +
        <basic name="status"/>
 +
        <basic name="rate">
 +
            <enumerated>STRING</enumerated>
 +
        </basic>
 +
        ...
 +
    </attributes>
 +
</entity>
 
</source>
 
</source>
  
Line 55: Line 74:
 
|section=Section 9.1.21 "Enumerated Annotation"}}
 
|section=Section 9.1.21 "Enumerated Annotation"}}
  
 +
====Legacy Data====
 +
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.
  
 +
See, [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Default_Conversions_and_Converters/ObjectTypeConverter|ObjectTypeConverter]]
  
 
{{EclipseLink_JPA
 
{{EclipseLink_JPA

Latest revision as of 14:00, 27 October 2011

EclipseLink JPA

Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug Day
Contribute
Browse Source


@Enumerated

By default, EclipseLink persistence provider persists the ordinal values of enumerated constants.

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

You can use this annotation with the @Basic annotation.

@Enumerated Attributes
Attribute Description Default Required?
value 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: Enumerated Constants
public enum EmployeeStatus {FULL_TIME, PART_TIME, CONTRACT}
public enum SalaryRate {JUNIOR, SENIOR, MANAGER, EXECUTIVE}
Example: @Enumerated Annotation
@Entity
public class Employee implements Serializable{
    ...
    public EmployeeStatus getStatus() {
        ...
    }
 
    @Enumerated(STRING)
    public SalaryRate getRate() {
        ...
    }
    ...
}
Example: Using <enumerated> XML
<entity class="Employee">
    <attributes>
        ...
        <basic name="status"/>
        <basic name="rate">
            <enumerated>STRING</enumerated>
        </basic>
        ...
    </attributes>
</entity>
Elug javaspec icon.gif

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.

See, ObjectTypeConverter

Eclipselink-logo.gif
Version: 2.2.0 DRAFT
Other versions...

Back to the top