I'm using Java 10. If I define an enum like this
public enum MyEnum {
NAME1("value 1"),
NAME2("value 2"),
...
}
and then I have a JPA-mapped database column that is defined by the enum
@Enumerated(value = EnumType.STRING)
private MyEnum status;
What else do I need to do to map the value of the enum (e.g. "value 2") to the column "status" instead of the name, which is getting mapped currently?
If JPA2 really means JPA 2.1 then
AttributeConverteris the perfect solution.In this case there is an entity
and an enumerated field
AccountTypeFinally, the instructed
AccountTypeConvertershould be likeWhen opening an
Accountthe generated SQL statements are like this
The previous example shows how to implement and register an attribute converter to a field of an enity. If you have multiple entities with the same type of enumerated field and all occurrences should be converted, then it is possible to auto-register the converter.
Auto-registered
AttributeConverterIn this case annotating the entity field is no longer necessary.