Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 如何翻译@Enumerated属性的名称?_Hibernate_Orm_Jpa - Fatal编程技术网

Hibernate 如何翻译@Enumerated属性的名称?

Hibernate 如何翻译@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

有两种枚举类型: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;
}
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>可能的副本