JAVA枚举类型复写ordinal和name值

本文介绍了使用Hibernate框架进行数据库字段映射时,如何通过@Enumerated注解配置枚举类型的两种方式:按名称(STRING)和按序号(ORDINAL)。此外,还探讨了如何自定义枚举类型的序号和名称。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

持久层用hibernate实现,在实体和数据库映射的时候,字段设为枚举有两种方式:

1
2
3
@Enumerated(EnumType.STRING)
@Column(name="invoice_type")
private InvoiceType invoiceType;
@Enumerated(EnumType.STRING)
这种的写法,是实体映射枚举的名字,即是枚举类的name属性。同时invoice_type字段在数据库中的属性为vachar类型
假设我的枚举类是以下内容:
1
2
3
4
5
6
public enum InvoiceType {
 
    ONE,
    TWO,
    THREE;
}

  那么将来数据里字段invoice_type的值就是 ONE或者 TWO 或者THREE;

再换另一种写法:

1
2
3
@Enumerated(EnumType.ORDINAL)
@Column(name="invoice_type")
private InvoiceType invoiceType;
1
 
1
@Enumerated(EnumType.ORDINAL) <br>这样的映射就会把枚举的ordinal的值赋给数据库的字段,大家都知道枚举的ordinary的值从0开始,以此往下数。这样的配置的话,数据库中
1
invoice_type这个字段的内容将来就是,0,1,2,3.。。。。。<br><br>上面的知识大家基本都知道了,讨论一下如果hibernate那层的配置没有别的方式,只能是
EnumType.STRING,
EnumType.ORDINAL
那么如果我们想改变枚举默认的ordinal和name该怎么办?废话不多说了,直接上代码对比:
未重写之前:
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public enum InvoiceType {

    INVALID(9, "你好1"),

    OPEN(8, "你好2"),

    UNOPEN(7, "你好3");

    String name;

    int value;

    private static Map<Integer, String> map = new HashMap<Integer, String>();

    static {
        for (InvoiceType type : values()) {
            map.put(type.ordinal(), type.name());
        }
    }

    private InvoiceType(int value, String name) {
        this.value = value;
        // 初始化map列表
        this.name = name;
    }
    
    public static void main(String[] args) {
        System.out.println(InvoiceType.getMap());
        
        System.out.println(InvoiceType.INVALID.ordinal());
        System.out.println(InvoiceType.INVALID.name());
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public static Map<Integer, String> getMap() {
        return map;
    }

    public static void setMap(Map<Integer, String> map) {
        InvoiceType.map = map;
    }
}

运行结果:

{0=INVALID, 1=OPEN, 2=UNOPEN}
0
INVALID

 

重写以后:


 1 import java.lang.reflect.Field;
 2 import java.util.HashMap;
 3 import java.util.Map;
 4 
 5 public enum InvoiceType1 {
 6 
 7     INVALID(9, "你好1"),
 8 
 9     OPEN(8, "你好2"),
10 
11     UNOPEN(7, "你好3");
12 
13     String name;
14 
15     int value;
16 
17     private static Map<Integer, String> map = new HashMap<Integer, String>();
18 
19     static {
20         for (InvoiceType1 type : values()) {
21             map.put(type.ordinal(), type.name());
22         }
23     }
24 
25     private InvoiceType1(int value, String name) {
26         this.value = value;
27         // 初始化map列表
28         this.name = name;
29         try {
30             Field fieldName = getClass().getSuperclass().getDeclaredField("ordinal");
31             fieldName.setAccessible(true);
32             fieldName.set(this, value);
33             fieldName.setAccessible(false);
34 
35             Field fieldName1 = getClass().getSuperclass().getDeclaredField("name");
36             fieldName1.setAccessible(true);
37             fieldName1.set(this, name);
38             fieldName1.setAccessible(false);
39 
40         } catch (Exception e) {
41         }
42     }
43     
44     public static void main(String[] args) {
45         System.out.println(InvoiceType1.getMap());
46         
47         System.out.println(InvoiceType1.INVALID.ordinal());
48         System.out.println(InvoiceType1.INVALID.name());
49     }
50 
51     public String getName() {
52         return name;
53     }
54 
55     public void setName(String name) {
56         this.name = name;
57     }
58 
59     public int getValue() {
60         return value;
61     }
62 
63     public void setValue(int value) {
64         this.value = value;
65     }
66 
67     public static Map<Integer, String> getMap() {
68         return map;
69     }
70 
71     public static void setMap(Map<Integer, String> map) {
72         InvoiceType1.map = map;
73     }
74 }

 

运行结果:

{7=你好3, 8=你好2, 9=你好1}
9
你好1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值