jakartaee / jakartaee/persistence
Implicit Enumerated Type with `@EnumeratedValue`
- Dominant language
- Java
- Stars
- 268
- Forks
- 78
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 13
Description
This is a new annotation added in 3.2 that allow users to define the enumerated value freely.
I have tried the following code fragment.
```java
public enum Gender {
MALE("M"), FEMALE("F");
@EnumeratedValue
private final String symbol;
Gender(String symbol) {
this.symbol = symbol;
}
}
```
And used it in the entity class like this.
```java
private Gender gender = Gender.MALE;
```
The similar codes worked well in Hibernate 7.x.
But I encountered issues when trying it in GlassFish 8.0.0-M14: https://github.com/eclipse-ee4j/glassfish/issues/25779, **which means I have to use it with the existing `@Enumrated`**?
The following is the `@EnumratedValue` javadoc:
> Specifies that an annotated field of a Java enum type is the source of database
> column values for an enumerated mapping. The annotated field must be declared final, and must be of type:
> * byte, short, or int for EnumType.ORDINAL, or
> * String for EnumType.STRING.
> The annotated field must not be null, and must hold a distinct value for each value of the enum type.
> Example:
>
> ```java
> enum Status {
> OPEN(0), CLOSED(1), CANCELLED(-1);
>
> @EnumeratedValue
> final int intValue;
>
> Status(int intValue) {
> this.intValue = intValue;
> }
> }
>
> ```
The annotated field value is limited to `short/int` and their wrapper types or `String`, I think the `@EnumeratedValue` annotation can resolve enumerated type(`ORDINAL` or `STRING`) **implicitly** without `@Enumerated` on the entity field.
Adding explicit `@Enumerated` could raise more conflicts, for example, use a `@Enumerated(ORDINAL)` on the entity enum field(eg. `gender`), and use `String` type value with the `@EnumeratedValue` in the enum class.
I do not find there is a constraint somewhere about using `@EnumeratedValue` with/without `@Enumerated`. If there is a definition in the specification, please make `@Enumerated` optional when using `@EnumeratedValue`.
Contributor guide
Research direction
Start by reading the @EnumeratedValue Javadoc and the enum/entity examples in this issue, then review the linked GlassFish issue 25779 for the observed behavior. Determine whether @EnumeratedValue should make @Enumerated optional and how conflicting ORDINAL or STRING declarations should be handled; done means the specification behavior is explicitly clarified.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- databases
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100