Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Basic Mappings/Enumerated"
m (→@Enumerated) |
m |
||
Line 1: | Line 1: | ||
{{EclipseLink_UserGuide|info=y|toc=n}} | {{EclipseLink_UserGuide|info=y|toc=n}} | ||
=@Enumerated= | =@Enumerated= | ||
+ | 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: | ||
+ | |||
+ | You can use this annotation with the [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings|@Basic annotation]]. | ||
+ | |||
+ | {{EclipseLink_AttributeTable | ||
+ | |caption=The <tt>@Enumerated</tt> annotation has the following attributes: | ||
+ | |content=<tr> | ||
+ | <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></td> | ||
+ | <td>No</td> | ||
+ | </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. | ||
+ | |||
+ | |||
+ | <span id="Example 18-12"></span> | ||
+ | ''''' Enumerated Constants''''' | ||
+ | <source lang="java"> | ||
+ | public enum EmployeeStatus {FULL_TIME, PART_TIME, CONTRACT} | ||
+ | public enum SalaryRate {JUNIOR, SENIOR, MANAGER, EXECUTIVE} | ||
+ | </source> | ||
+ | |||
+ | <span id="Example 18-13"></span> | ||
+ | ''''' Usage of the @Enumerated Annotation''''' | ||
+ | <source lang="java"> | ||
+ | @Entity | ||
+ | public class Employee implements Serializable{ | ||
+ | ... | ||
+ | public EmployeeStatus getStatus() { | ||
+ | ... | ||
+ | } | ||
+ | |||
+ | @Enumerated(STRING) | ||
+ | public SalaryRate getRate() { | ||
+ | ... | ||
+ | } | ||
+ | ... | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | {EclipseLink_Spec | ||
+ | |seciton=Section 9.1.21 "Enumerated Annotation"}} | ||
Revision as of 09:48, 21 June 2010
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
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 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.
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 Usage of the @Enumerated Annotation example, 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. |
No |
Given the enumerated constants in the Enumerated Constants example, the Usage of the @Enumerated Annotation example 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.
Enumerated Constants
public enum EmployeeStatus {FULL_TIME, PART_TIME, CONTRACT} public enum SalaryRate {JUNIOR, SENIOR, MANAGER, EXECUTIVE}
Usage of the @Enumerated Annotation
@Entity public class Employee implements Serializable{ ... public EmployeeStatus getStatus() { ... } @Enumerated(STRING) public SalaryRate getRate() { ... } ... }
{EclipseLink_Spec |seciton=Section 9.1.21 "Enumerated Annotation"}}