JPA Enum ORDINAL vs STRING

后端 未结 8 1542
予麋鹿
予麋鹿 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 13:54

    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.

提交回复
热议问题