更改枚举的默认休眠映射策略
使用 Hibernate 时,枚举的默认映射策略是 ORDINAL。因为我认为 STRING 策略更强大,所以我想更改默认映射策略以支持它。
目前我必须在任何枚举上使用以下内容:
@Enumerated(EnumType.STRING)
@Column(nullable=false)
private MyEnum myEnum;
有没有办法告诉 hibernate 始终使用 STRING 策略?我的研究没有提出一些可以改变这种行为的属性。
或者这是不可能的,因为 JPA 将 ORDINAL 指定为默认值,并且我们应该显式注释它以保持 O/R-Mapper 中立?
When using Hibernate, the dafault mapping strategy for enums is ORDINAL. Because I think the STRING strategy is more robust, I want to change the default mapping strategy to favor that.
Currently I have to use the following on any enum:
@Enumerated(EnumType.STRING)
@Column(nullable=false)
private MyEnum myEnum;
Is there a way to tell hibernate to always use the STRING strategy? My research did not come up with some property that allows to change this behaviour.
Or is it impossible because JPA specifies ORDINAL as default and we should explicitly annotate it to stay O/R-Mapper neutral?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我同意你的观点,
EnumType.STRING
更加健壮,尤其是在重构源代码时,因为枚举中常量的更改顺序通常是由于疏忽而完成的(或者仅仅是因为有人按字母顺序对它们进行排序)。但您也认为无法更改默认行为,因为 ORDINAL 映射已按设计定义为默认行为(可能是因为数字列比 varchar 类型列更有效)。
I agree with you that
EnumType.STRING
is more robust especially when refactoring source code as chaging order of constants in enum is often done by inadvertance (or simply because someone sorts them alphabeticaly).But you also right that there is no way to change the default behaviour, as ORDINAL mapping has been defined as default by design (may be because a Numeric Column is more efficient than a varchar typed column).