Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persistence of Enums is quite limited and would benefit from more flexibility #81

Closed
andyjefferson opened this issue May 19, 2016 · 0 comments

Comments

@andyjefferson
Copy link
Member

andyjefferson commented May 19, 2016

Currently JDO and JPA allow persisting as "name()" (String) or "ordinal()" (Integer) of the Enum. DataNucleus (RDBMS) allows control over the ordinal value to be persisted, like this

public enum MyColour 
{
    RED((short)1), GREEN((short)3), BLUE((short)5), YELLOW((short)8);
    private short value;

    private MyColour(short value)
    {
            this.value = value;
    }
    public short getValue() 
    {
            return value;
    }
    public static MyColour getEnumByValue(short value)
    {
        switch (value)
        {
            case 1:
                return RED;
            case 3:
                return GREEN;
            case 5:
                return BLUE;
            default:
                return YELLOW;
        }
    }
}
@Extensions({
    @Extension(key="enum-getter-by-value", value="getEnumByValue"),
    @Extension(key="enum-value-getter", value="getValue")
   })
MyColour colour;

Maybe we would benefit from allowing control over either ordinal or name, so we could have

public enum Language {
  ENGLISH_US("en-US"),
  ENGLISH_BRITISH("en-BR"),
  FRENCH("fr"),
  FRENCH_CANADIAN("fr-CA");
  private String code;

  Language(String code) {
    this.code = code;
  }
  public String getCode() {
    return code;
  }
}

or something similar? Additionally it should be made available for ALL datastores not just RDBMS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant