0
有两个EnumType:如EnumType.ORDINAL
和EnumType.STRING
。但是都有disvantages:如何翻译@Enumerated属性的名称?
EnumType.ORDINAL: You must preserve the enum order.
EnumType.STRING: The column width is the max length of the enum names, which maybe very long.
相反,我想介绍一个缩写为枚举类型:
public Enum MonthEnum {
January("JAN"),
February("FEB"),
March("MAR"),
...;
String abbrev;
static Map<String, MonthEnum> abbrevs = new HashMap();
MonthEnum(String abbrev) {
this.abbrev = abbrev;
abbrevs.put(abbrev, this);
}
public static MonthEnum fromAbbrev(String abbrev) {
return abbrevs.get(abbrev);
}
}
@Entity
class Diary {
@Enumerated(ABBREV)
Month month;
}
[使用Hibernate注释来保留枚举属性的自定义值](http://stackoverflow.com/questions/5032116/using-hibernate-annotations-to-persist-custom-value-for-enumerated-attribute ) – axtavt 2011-05-16 09:00:37