Mapping Enum to a String column uses toString() instead of name()
See original GitHub issueI have a field in an entity like
...
@Type( type = "pgsql_enum" )
@Enumerated(EnumType.STRING)
private MyEnum myEnum;
...
where MyEnum
is defined as
public enum MyEnum{
PROP_A {
@Override
public String toString() {
return "Prop A";
}
}
}
I expected that in my db the field would be PROP_A
but I find that it is Prop A
which indicates that the conversion is using the toString()
representation of enum whereas I believe name()
would be more appropriate since toString()
is overridable and name()
is not.
Is there a workaround or a configuration property I have overlooked?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Mapping enum to string in hibernate - java - Stack Overflow
Yes, is possible. It should be: @Enumerated(EnumType.STRING) @Column(name = "category_type") private CategoryType categoryType;.
Read more >Enum Mappings with Hibernate - The Complete Guide
With JPA and Hibernate, you can map enums in different ways. You can: use the standard mappings to a number or a String,;...
Read more >Enum.ToString Method (System) - Microsoft Learn
Converts the value of this instance to its equivalent string representation.
Read more >Persisting Enums in JPA - Baeldung
Analogously, JPA will use the Enum.name() value when storing an entity if we annotate the enum field with @Enumerated(EnumType.STRING).
Read more >MySQL 8.0 Reference Manual :: 11.3.5 The ENUM Type
Creating and Using ENUM Columns. An enumeration value must be a quoted string literal. For example, you can create a table with an...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The issue can be easily replicated by overriding the
toString()
method of an enum member. This causes whatever is returned in thetoString()
to be saved in the db. This has an additional effect of not being able to query the record anymore since the string returned viatoString()
cannot be resolved byEnum.valueOf()
. If you still need a reproduction, a template project would help me provide one.You need to send a replicating test case to prove the problem. This is a mandatory step in order to fix any issue.