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.

A way to use custom string values to map enums?

See original GitHub issue

I have an enum like this:

public enum MyEnum {
  DESCRIPTIVE_NAME1("short_code1"),
  DESCRIPTIVE_NAME2("short_code2"),
  ...
  MyEnum(String shortCode) {
     this.shortCode = shortCode;
  }
  ...
  public static MyEnum fromShortCode(String shortCode) {
    // lookup from MyEnum.values() using shortCode
  }
}

And a class with MyEnum field. The corresponding table column holds shortCode values. Is there a way to use MyEnum.fromShortCode() when reading the value from the result set? Right now anything I do doesn’t seem to have an effect and org.simpleflatmapper.converter.impl.CharSequenceToEnumConverter.convert() gets called no matter what. This of course calls java.lang.Enum.valueOf() and mapping fails. I use SFM with JdbcTemplate and tried using both JdbcTemplateMapperFactory.addColumnDefinition() and the ServiceLoader/Converter approach. Any working example on how to achieve this? I imagine this is a fairly common use case, since other libraries like Jackson provide a special way to serialize/deserialize enums not only by their name.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:16 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
arnaudrogercommented, Mar 27, 2019

6.6.0 is being release at the minute and should work fine now with springboot devtools.

1reaction
arnaudrogercommented, Mar 25, 2019

@neVERberleRfellerER created issue 623 to see if we can support the dev tools reload

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Conveniently map between enum and int / String
It is relatively easy to create a static map with the key values. ... call it's new method to convert an Enum to...
Read more >
Attaching Values to Java Enum - Baeldung
Java provides a valueOf(String) method for all enum types. Thus, we can always get an enum value based on the declared name: assertSame(Element....
Read more >
Enum Mappings with Hibernate - The Complete Guide
You can map the enum value to a String. This mapping is not as efficient, but you can add or remove enum values...
Read more >
String Enums in C#: Everything You Need to Know
Use a public static readonly string; Custom Enumeration Class; Attribute and Extension methods; Use a Switch statement; Map Enum Values ...
Read more >
Handbook - Enums - TypeScript
String enums allow you to give a meaningful and readable value when your code runs, independent of the name of the enum member...
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