JPA枚举ORDINAL vs STRING [英] JPA Enum ORDINAL vs STRING
问题描述
可以使用
@Enumerated(EnumType.ORDINAL)
或
@Enumerated(EnumType.STRING)
我不知道这两个定义有什么优缺点?
I wonder what are advantages and disadvantages of those two definitions?
我听说ORDINAL使用EclipseLink比STRING更好(更快)。
这是真的吗?
I heard that ORDINAL performs better (is faster) than STRING with EclipseLink.
Is that true?
推荐答案
我总是去 STRING
。
很少是最重要的问题 - 可读性和可维护性更重要。
Speed is rarely the most important issue - readability and maintainability are more important.
我使用 STRING
因为手动检查数据库中的行很容易,更重要的是,我可以做两件事情,而不用触摸数据库, ORDINAL
无法处理:
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
这两个更改都将改变序号枚举已经在数据库中使用,因此如果您使用 ORDINAL
,则破坏现有数据。
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';
这篇关于JPA枚举ORDINAL vs STRING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!