In JPA (Java Persistence API), when you map an enum type to a database column, you have two common options for how the enum values are stored in the database: ORDINAL and STRING. These options determine how the enum values are persisted and can impact the behavior of your JPA entities.
ORDINAL (Default):
When you use the ORDINAL mapping strategy (the default), JPA maps the enum values to their ordinal positions (0-based integers) in the enum's declaration order. In other words, the database stores the numeric index of the enum value.
Example:
public enum Status { PENDING, APPROVED, REJECTED } In this case, PENDING would be stored as 0, APPROVED as 1, and REJECTED as 2.
Pros:
ORDINAL uses integers, which can be more space-efficient than storing the enum values as strings.Cons:
STRING:
When you use the STRING mapping strategy, JPA maps the enum values to their string representations. In other words, the database stores the enum values as strings.
Example:
public enum Status { PENDING, APPROVED, REJECTED } In this case, PENDING would be stored as "PENDING", APPROVED as "APPROVED", and REJECTED as "REJECTED".
Pros:
Cons:
ORDINAL.To specify the mapping strategy, you can use JPA annotations like @Enumerated(EnumType.STRING) or @Enumerated(EnumType.ORDINAL) on the enum attribute in your entity class. For example:
@Entity public class MyEntity { @Enumerated(EnumType.STRING) private Status status; // Other entity properties and methods... } In summary, the choice between ORDINAL and STRING mapping strategies depends on your specific use case and trade-offs. If space efficiency and query performance are critical, ORDINAL may be preferable. If data readability and robustness are more important, STRING is a better choice, especially when dealing with databases that are expected to be human-readable or when the enum values are likely to change.
doctrine-orm construct google-chrome angular-cdk datagridview lsusb scipy-spatial globalization progress-db reference