持久层用hibernate实现,在实体和数据库映射的时候,字段设为枚举有两种方式:
@EnumeratedEnumType.STRING) @Columnname="invoice_type") private InvoiceType invoiceType;
@EnumeratedEnumType.STRING)
这种的写法,是实体映射枚举的名字,即是枚举类的name属性。同时invoice_type字段在数据库中的属性为vachar类型
假设我的枚举类是以下内容:
public enum InvoiceType { ONE, TWO, THREE; }
那么将来数据里字段invoice_type的值就是 ONE或者 TWO 或者THREE;
再换另一种写法:
@EnumeratedEnumType.ORDINAL) @Columnname="invoice_type") private InvoiceType invoiceType;
@EnumeratedEnumType.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 { INVALID9, "你好1"), OPEN8, "你好2"), UNOPEN7, "你好3"); String name; int value; private static Map<Integer, String> map = new HashMap<Integer, String>); static { for InvoiceType type : values)) { map.puttype.ordinal), type.name)); } } private InvoiceTypeint value, String name) { this.value = value; // 初始化map列表 this.name = name; } public static void mainString[] args) { System.out.printlnInvoiceType.getMap)); System.out.printlnInvoiceType.INVALID.ordinal)); System.out.printlnInvoiceType.INVALID.name)); } public String getName) { return name; } public void setNameString name) { this.name = name; } public int getValue) { return value; } public void setValueint value) { this.value = value; } public static Map<Integer, String> getMap) { return map; } public static void setMapMap<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 INVALID9, "你好1"), 8 9 OPEN8, "你好2"), 10 11 UNOPEN7, "你好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.puttype.ordinal), type.name)); 22 } 23 } 24 25 private InvoiceType1int value, String name) { 26 this.value = value; 27 // 初始化map列表 28 this.name = name; 29 try { 30 Field fieldName = getClass).getSuperclass).getDeclaredField"ordinal"); 31 fieldName.setAccessibletrue); 32 fieldName.setthis, value); 33 fieldName.setAccessiblefalse); 34 35 Field fieldName1 = getClass).getSuperclass).getDeclaredField"name"); 36 fieldName1.setAccessibletrue); 37 fieldName1.setthis, name); 38 fieldName1.setAccessiblefalse); 39 40 } catch Exception e) { 41 } 42 } 43 44 public static void mainString[] args) { 45 System.out.printlnInvoiceType1.getMap)); 46 47 System.out.printlnInvoiceType1.INVALID.ordinal)); 48 System.out.printlnInvoiceType1.INVALID.name)); 49 } 50 51 public String getName) { 52 return name; 53 } 54 55 public void setNameString name) { 56 this.name = name; 57 } 58 59 public int getValue) { 60 return value; 61 } 62 63 public void setValueint value) { 64 this.value = value; 65 } 66 67 public static Map<Integer, String> getMap) { 68 return map; 69 } 70 71 public static void setMapMap<Integer, String> map) { 72 InvoiceType1.map = map; 73 } 74 }
View Code
运行结果:
{7=你好3, 8=你好2, 9=你好1}
9
你好1