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

Can we serialize Enum[] to String[] instead of int[]? #71

Open
ph4t opened this issue Jul 30, 2015 · 3 comments
Open

Can we serialize Enum[] to String[] instead of int[]? #71

ph4t opened this issue Jul 30, 2015 · 3 comments

Comments

@ph4t
Copy link

ph4t commented Jul 30, 2015

Currently the implementation will serialize an array of enums to an array of ordinal values.
Using the Juice enum example from the documentation: [Juice.ORANGE, Juice.GRAPE] will be stored as [0, 2]

I think there are a few difficulties with this approach:

  1. If the order of enums is changed, this will change the ordinal values. As result, aren't you going to get wrong enums when existing record are read? E.g.:
    Assume we have [0,2] stored. If we change the enum to
static enum Juice {
        ORANGE(0),
        GRAPE(2),
        APPLE(1)
}

Shouldn't we get [Juice.ORANGE, Juice.APPLE] instead of original [Juice.ORANGE, Juice.GRAPE] ?

Btw, if ordinal values are used, why do we have value field in Juice? Aren't these 2 unrelated?

  1. If the table is used by other teams/tools we need to share mapping of our ordinal values to enum names with them. It would be much more descriptive if we just had varchars...
  2. GORM will map an Enum field to a varchar type. So it would make more sense to map Enum[] to varchar[]
@ilopmar
Copy link
Collaborator

ilopmar commented Jan 1, 2016

Hi.

First of all I'm sorry for taking too much time to answer this issue.

Currently I'm working on migrating all the Java code to Groovy + @CompileStatic and I'm studying the file in which these mappings are done BidiEnumMap.

I'm not the original author of this code and I don't remember why we did this but after analyzing the code I think you're right. It seems strange to store the ids instead of just storing the value itself.

I'll ask with @Alotor after my holidays because he did the code but I think we should change this to just store the value.

@edwardotis
Copy link

+1 for String[]
Storing enums as positional ints is a dangerous database anti-pattern.

@pytsekas
Copy link

Is there solution for this?
Same as 102

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

4 participants