JPA Enum ORDINAL vs STRING
Solution 1:
I always go STRING
.
Speed is rarely the most important issue - readability and maintainability are more important.
I use STRING
because it's a lot easier to manually inspect rows from the database, but more importantly, I can do two things, without touching the database, the ORDINAL
can't handle:
- I can change the order of my enums
- I can insert new enums in the middle of the enum list
Both of these changes will alter the ordinal values of the enums already in use in the database, thus breaking existing data if you are using ORDINAL
.
If you change an enum value (not that common), handling it is simple:
UPDATE table SET enum_column = 'NEW_ENUM_NAME' where enum_column = 'OLD_ENUM_NAME';
Solution 2:
It's likely that ORDINAL
is more efficient, but that's minor. There are a few downsides to ORDINAL
:
- it is less readable in the database
- if you reorder your enum definitions the database will not be consistent.
With STRING
you can't rename your enums.
Pick one of them and use it throughout the whole application - be consistent.
If your database is going to be used by other clients/languages - use STRING
, it's more readable.