JPA Enum ORDINAL vs STRING

后端 未结 8 1733
予麋鹿
予麋鹿 2021-02-01 13:32

It\'s possible to define enumerations in JPA using either

@Enumerated(EnumType.ORDINAL)

or

@Enumerated(EnumType.STRING)
         


        
8条回答
  •  感动是毒
    2021-02-01 14:01

    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:

    1. I can change the order of my enums
    2. 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';
    

提交回复
热议问题