有两个EnumType: as EnumType.ORDINAL和as EnumType.STRING。然而,两者都有缺点:
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;
}发布于 2011-06-12 00:20:23
在JPA 2.0中无法做到这一点。但它可以在Hibernate中使用UserType来完成。但你为什么要。我已经考虑过你的字符串变量的参数,但这个参数不适用于数据库中的任何字符串(varchar)列。还请考虑到枚举可能会在JAVA代码中使用,因此很长的枚举字符串值也会有问题。
https://stackoverflow.com/questions/6014340
复制相似问题