Hibernate 如何翻译@Enumerated属性的名称?
有两种枚举类型:asHibernate 如何翻译@Enumerated属性的名称?,hibernate,orm,jpa,Hibernate,Orm,Jpa,有两种枚举类型:asEnumType.ORDINAL和asEnumType.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 { Ja
EnumType.ORDINAL
和asEnumType.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;
}
public Enum MonthEnum{
一月(“一月”),
二月(“二月”),
三月(“三月”),
...;
字符串缩写;
静态映射abbrevs=newhashmap();
蒙特纳姆(字符串缩写){
this.abbrev=abbrev;
abbrevs.put(abbrev,this);
}
公共静态MonthEnum fromAbbrev(字符串abbrev){
返回abbrevs.get(abbrev);
}
}
@实体
课堂日记{
@经点算(缩写)
月-月;
}
这在JPA2.0中无法完成。但是它可以在Hibernate中使用UserType完成。但你为什么要这么做。我已经考虑过字符串变量的参数,但是这个参数不适用于数据库中的任何字符串(varchar)列。也请考虑枚举可能会在java代码中使用,因此长枚举字符串值也将是一个问题。< /P>可能的副本