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

From Eclipsepedia

Jump to: navigation, search
(@Enumerated)
Line 18: Line 18:
 
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:
 
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

Revision as of 13:55, 27 October 2011

EclipseLink JPA

link="http://wiki.eclipse.org/EclipseLink"
EclipseLink
Website
Download
Community
Mailing ListForumsIRC
Bugzilla
Open
Help Wanted
Bug 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">
        </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...