zoukankan      html  css  js  c++  java
  • JAVA枚举类型复写ordinal和name值

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

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

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

    再换另一种写法:

        @Enumerated(EnumType.ORDINAL)
        @Column(name="invoice_type")
        private InvoiceType invoiceType;
    
    
    @Enumerated(EnumType.ORDINAL) 
    这样的映射就会把枚举的ordinal的值赋给数据库的字段,大家都知道枚举的ordinary的值从0开始,以此往下数。这样的配置的话,数据库中
    invoice_type这个字段的内容将来就是,0,1,2,3.。。。。。

    上面的知识大家基本都知道了,讨论一下如果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;
        }
    }
    View Code

    运行结果:

    {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 }
    View Code

    运行结果:

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

  • 相关阅读:
    继续OI
    [WARNING]考前必读?!
    近些日的总结吧
    续上文
    又是一年NOIP然鹅我考的是高数(虽然我没打并且内容与NOIP无关)(手动滑稽)
    轮船问题(DP基础)
    NOIP2016报零记
    字符数组
    HA-0302 退役
    各种模板(part 2)
  • 原文地址:https://www.cnblogs.com/jqlbj/p/6209852.html
Copyright © 2011-2022 走看看