question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mapping Enum to a String column uses toString() instead of name()

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
SayakMukhopadhyaycommented, Sep 26, 2019

The issue can be easily replicated by overriding the toString() method of an enum member. This causes whatever is returned in the toString() to be saved in the db. This has an additional effect of not being able to query the record anymore since the string returned via toString() cannot be resolved by Enum.valueOf(). If you still need a reproduction, a template project would help me provide one.

2reactions
vladmihalceacommented, Sep 26, 2019

You need to send a replicating test case to prove the problem. This is a mandatory step in order to fix any issue.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found