Skip to content
Advertisement

Ways to save enums in database

What is the best way to save enums into a database?

I know Java provides name() and valueOf() methods to convert enum values into a String and back. But are there any other (flexible) options to store these values?

Is there a smart way to make enums into unique numbers (ordinal() is not safe to use)?

Update

Thanks for all awesome and fast answers! It was as I suspected.

However, a note to toolkit: That is one way. The problem is that I would have to add the same methods to each enum type that I create. That’s a lot of duplicated code and, at the moment, Java does not support any solutions for this (a Java enum cannot extend other classes).

Advertisement

Answer

We never store enumerations as numerical ordinal values anymore; it makes debugging and support way too difficult. We store the actual enumeration value converted to string:

JavaScript

and then read back with:

JavaScript

The problem was in the past staring at Enterprise Manager and trying to decipher:

JavaScript

verses

JavaScript

the latter is much easier. The former required getting at the source code and finding the numerical values that were assigned to the enumeration members.

Yes it takes more space, but the enumeration member names are short, and hard drives are cheap, and it is much more worth it to help when you’re having a problem.

Additionally, if you use numerical values, you are tied to them. You cannot nicely insert or rearrange the members without having to force the old numerical values. For example, changing the Suit enumeration to:

JavaScript

would have to become :

JavaScript

in order to maintain the legacy numerical values stored in the database.

How to sort them in the database

The question comes up: lets say i wanted to order the values. Some people may want to sort them by the enum‘s ordinal value. Of course, ordering the cards by the numerical value of the enumeration is meaningless:

JavaScript

That’s not the order we want – we want them in enumeration order:

JavaScript

The same work that is required if you save integer values is required if you save strings:

JavaScript

But that’s not the order we want – we want them in enumeration order:

JavaScript

My opinion is that this kind of ranking belongs in the user interface. If you are sorting items based on their enumeration value: you’re doing something wrong.

But if you wanted to really do that, i would create a Suits dimension table:

Suit SuitID Rank Color
Unknown 4 0 NULL
Heart 1 1 Red
Club 3 2 Black
Diamond 2 3 Red
Spade 0 4 Black

This way, when you want to change your cards to use Kissing Kings New Deck Order you can change it for display purposes without throwing away all your data:

Suit SuitID Rank Color CardOrder
Unknown 4 0 NULL NULL
Spade 0 1 Black 1
Diamond 2 2 Red 1
Club 3 3 Black -1
Heart 1 4 Red -1

Now we are separating an internal programming detail (enumeration name, enumeration value) with a display setting meant for users:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement