hibernate字段映射枚举类型
上一篇介绍了mybatis字段映射枚举类型,这一篇给大家介绍一下hibernate字段怎么去映射枚举类型的(这只是一种参考方式,映射方法还有很多种)。
还是以上篇sku表为例,sku表里一个status_type字段为int类型。(这里是postgresql的脚本)
1 status_type int4 NULL
Java里面对应生成的枚举类为:
1 public enum SkuStatusType implements DbEnum, LocalString { 2 3 PRODUCT(0, false, "masterdata.skuStatus.product"), 4 BUYING(1, false, "masterdata.skuStatus.buying"), 5 SALE(2, false, "masterdata.skuStatus.sale"); 6 7 8 private int dbConstant; 9 private String messageKey; 10 11 private OrderState(int dbConstant, boolean hasBeenDispatched, String messageKey) { 12 this.dbConstant = dbConstant; 13 this.hasBeenDispatched = hasBeenDispatched; 14 this.messageKey = messageKey; 15 } 16 17 @Override 18 public Integer getConstant() { 19 return this.dbConstant; 20 } 21 22 @Override 23 public String messageKey() { 24 return this.messageKey; 25 } 26 }
接下来是和mybatis不同的地方,所写的工具类不一样,看一下为hibernate写的工具类:
1 package com.lcc.core.domain; 2 3 import java.io.Serializable; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Types; 8 import java.util.HashMap; 9 import java.util.Map; 10 import java.util.Properties; 11 12 import org.hibernate.HibernateException; 13 import org.hibernate.usertype.ParameterizedType; 14 import org.hibernate.usertype.UserType; 15 import org.hibernate.util.ReflectHelper; 16 17 import com.lcc.api.domain.DbEnum; 18 19 public class DbEnumType implements UserType, ParameterizedType { 20 21 private static final int[] SQL_TYPES = { Types.INTEGER }; 22 23 private Class<? super Enum<?>> enumClass; 24 private Map<Integer, Object> constantMap = null; 25 26 @Override 27 public int[] sqlTypes() { 28 return SQL_TYPES; 29 } 30 31 @Override 32 @SuppressWarnings("rawtypes") 33 public Class returnedClass() { 34 return enumClass; 35 } 36 37 @Override 38 public boolean equals(Object x, Object y) { 39 if (x == y) { 40 return true; 41 } 42 if (x == null || y == null) { 43 return false; 44 } 45 return x.equals(y); 46 } 47 48 @Override 49 public int hashCode(Object x) { 50 return x.hashCode(); 51 } 52 53 @Override 54 public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws SQLException { 55 Integer value = rs.getInt(names[0]); 56 if (rs.wasNull()) { 57 return null; 58 } else { 59 return getEnumConstant(value); 60 } 61 } 62 63 64 private Object getEnumConstant(Integer value) { 65 if (constantMap == null) { 66 Map constantMap = new HashMap<Integer, Object>(); 67 68 Object[] enumConstants = enumClass.getEnumConstants(); 69 for (Object enumC : enumConstants) { 70 constantMap.put(((DbEnum) enumC).getConstant(), enumC); 71 } 72 this.constantMap = constantMap; 73 } 74 return constantMap.get(value); 75 } 76 77 @Override 78 public void nullSafeSet(PreparedStatement st, Object value, int index) throws SQLException { 79 if (value == null) { 80 st.setNull(index, Types.INTEGER); 81 } else { 82 DbEnum e = (DbEnum) value; 83 st.setInt(index, e.getConstant()); 84 } 85 } 86 87 @Override 88 public Object deepCopy(Object value) { 89 if (value == null) { 90 return null; 91 } else { 92 return value; 93 } 94 } 95 96 @Override 97 public boolean isMutable() { 98 return false; 99 } 100 101 @Override 102 public Serializable disassemble(Object value) { 103 return (DbEnum) value; 104 } 105 106 @Override 107 public Object assemble(Serializable cached, Object owner) { 108 return cached; 109 } 110 111 @Override 112 public Object replace(Object original, Object target, Object owner) { 113 return original; 114 } 115 116 @Override 117 @SuppressWarnings("unchecked") 118 public void setParameterValues(Properties parameters) { 119 String enumClassName = parameters.getProperty("enumClass"); 120 try { 121 enumClass = ReflectHelper.classForName(enumClassName, this.getClass()).asSubclass(Enum.class); 122 if (!ReflectHelper.implementsInterface(enumClass, DbEnum.class)) { 123 throw new HibernateException("Enum does not implement DBEnum"); 124 } 125 } catch (ClassNotFoundException exception) { 126 throw new HibernateException("Enum class not found", exception); 127 } 128 } 129 }
上面工具类里面的interface(DbEnum, LocalString)请参考上一篇mybatis里面的:http://www.cnblogs.com/cc-java/p/6593589.html
工具类写好了 接下来需要在对象里面注解属性(对象是基于annotations的):
@NotNull @Column(nullable = false) @Index(name = "order_state_idx") @Type(type = "com.lcc.core.domain.DbEnumType", parameters = { @Parameter(name = "enumClass", value = "com.lcc.api.domain.enums.order.SkuStatusType") }) private SkuStatusType state;
到这里差不多hibernate字段映射枚举类型就完成了。