Hibernate 實現Enum轉換爲Smallint

由於數據庫的原因,需要用smallint來存儲枚舉類型.

Hibernate 3.0 以上也支持 Enum類型的轉換,這裏以Smallint爲例(當然,也可以轉換爲其他類型,如varchar).

 

首先,以下是一個枚舉類型

  1. public enum ConsumerType
  2. {
  3.     Admin,Vistor,VIP;
  4.     
  5. }

然後,再寫一個模板,實現UserType類型接口,這樣就可以將以後寫的枚舉類型都轉換爲Smallint存入數據庫

 

  1. import java.io.Serializable;
  2. import java.sql.PreparedStatement;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Types;
  6. import org.hibernate.HibernateException;
  7. import org.hibernate.usertype.UserType;
  8. public class IntEnumUserType<E extends Enum<E>> implements UserType 
  9.     private Class<E> clazz = null;
  10.     private E[] theEnumValues;
  11.     protected IntEnumUserType(Class<E> c, E[] e) 
  12.     { 
  13.         this.clazz = c; 
  14.         this.theEnumValues = e;
  15.     } 
  16.     private static final int[] SQL_TYPES = {Types.SMALLINT};
  17.     public int[] sqlTypes() 
  18.     { 
  19.         return SQL_TYPES; 
  20.     } 
  21.     public Class<E> returnedClass() 
  22.     { 
  23.         return clazz; 
  24.     } 
  25.     public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) 
  26.     throws HibernateException, SQLException 
  27.     { 
  28.         final int val = resultSet.getShort(names[0]);
  29.         E result = null;
  30.         if (!resultSet.wasNull()) 
  31.         {
  32.             try 
  33.             {
  34.                 for(int i=0; i < theEnumValues.length && result == null; i++) 
  35.                 {
  36.                     if (theEnumValues[i].ordinal() == val) 
  37.                     {
  38.                         result = theEnumValues[i];
  39.                     }
  40.                 }
  41.             } 
  42.             catch (SecurityException e) 
  43.             {
  44.                 result = null;
  45.             } 
  46.             catch (IllegalArgumentException e) 
  47.             {
  48.                 result = null;
  49.             }
  50.         } 
  51.         return result; 
  52.     } 
  53.     public void nullSafeSet(PreparedStatement preparedStatement, 
  54.             Object value, int index) throws HibernateException, SQLException 
  55.             { 
  56.         if (null == value) 
  57.         { 
  58.             preparedStatement.setNull(index, Types.SMALLINT); 
  59.         } 
  60.         else 
  61.         { 
  62.             preparedStatement.setInt(index, ((Enum)value).ordinal()); 
  63.         } 
  64.             } 
  65.     public Object deepCopy(Object value) throws HibernateException
  66.     { 
  67.         return value; 
  68.     } 
  69.     public boolean isMutable() 
  70.     { 
  71.         return false
  72.     } 
  73.     public Object assemble(Serializable cached, Object owner) throws HibernateException 
  74.     {
  75.         return cached;
  76.     } 
  77.     public Serializable disassemble(Object value) throws HibernateException 
  78.     { 
  79.         return (Serializable)value; 
  80.     } 
  81.     public Object replace(Object original, Object target, Object owner) throws HibernateException 
  82.     { 
  83.         return original; 
  84.     } 
  85.     public int hashCode(Object x) throws HibernateException 
  86.     { 
  87.         return x.hashCode(); 
  88.     } 
  89.     public boolean equals(Object x, Object y) throws HibernateException
  90.     { 
  91.         if (x == y) 
  92.             return true
  93.         if (null == x || null == y) 
  94.             return false
  95.         return x.equals(y); 
  96.     } 

跟着,新建一個類來實現模板

 

  1. /** This class is used only in the hibernate XML configuration */
  2. public class ConsumerTypeEnum extends IntEnumUserType<ConsumerType> 
  3.     public ConsumerTypeEnum() 
  4.     { 
  5.         // we must give the values of the enum to the parent.
  6.         super(ConsumerType.class,ConsumerType.values()); 
  7.     } 
  8. }

最後,在mapping的xml文件中,使用如下代碼

 

  1. <property column="USERTPYE" 
  2. not-null="true" 
  3. name="consumerType"
  4. type="ConsumerTypeEnum" />

注意,這裏是使用 ConsumerTypeEnum 而不是 ConsumerType

 

到此,轉換完成~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章