我有一个存储枚举字符串值的mysql表。
我的模型有这样的注解:
@Enumerated(EnumType.STRING)我的枚举看起来像这样:
public enum SomeEnum {
BLUE(0),
YELLOW(1),
RED(2),
private int value;
public SomeEnum(int value) {
this.value = value;
}
}我希望我能以某种方式将枚举的整数' value‘存储在我的数据库中,然后在hibernate将我的模型加载到java类中时,以某种方式使用枚举的值重新解析它。
我不想使用序数值,因为它不够稳定。我也不想使用mysql枚举类型,因为它毫无理由地将我绑定到mysql。
那么这是可能的吗?
发布于 2012-04-09 23:32:58
我猜你总是可以映射你自己的用户类型:http://alenovarini.wikidot.com/mapping-a-custom-type-in-hibernate
发布于 2012-04-09 23:33:09
为什么不尝试使用int类型(未枚举)的字段,如果需要,只需从已有的Enum类型获取/设置它?
发布于 2014-03-24 08:53:30
看起来这是可能的。我尝试了一下来自:http://blog.tamashumi.com/2013/06/grails-enum-custom-database-value.html的这个技巧,看起来很管用:
enum DayTime {
Morning(0),Noon(1),AfterNoon(2),Night(3)
final int id
private DayTime(int id) { this.id = id }
}然后在你的域类中:
DayTime dayTimehttps://stackoverflow.com/questions/10075496
复制相似问题