Hibernate實(shí)現(xiàn)Enum轉(zhuǎn)換為Smallint

字號(hào):

Hibernate 3.0 以上也支持 Enum類型的轉(zhuǎn)換,這里以Smallint為例(當(dāng)然,也可以轉(zhuǎn)換為其他類型,如varchar).
    首先,以下是一個(gè)枚舉類型
    public enum ConsumerType
    {
    Admin,Vistor,VIP;
    }
    然后,再寫一個(gè)模板,實(shí)現(xiàn)UserType類型接口,這樣就可以將以后寫的枚舉類型都轉(zhuǎn)換為Smallint存入數(shù)據(jù)庫
    import java.io.Serializable;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Types;
    import org.hibernate.HibernateException;
    import org.hibernate.usertype.UserType;
    public class IntEnumUserType> implements UserType
    {
    private Class clazz = null;
    private E[] theEnumValues;
    protected IntEnumUserType(Class c, E[] e)
    {
    this.clazz = c;
    this.theEnumValues = e;
    }
    private static final int[] SQL_TYPES = {Types.SMALLINT};
    public int[] sqlTypes()
    {
    return SQL_TYPES;
    }
    public Class returnedClass()
    {
    return clazz;
    }
    public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
    throws HibernateException, SQLException
    {
    final int val = resultSet.getShort(names[0]);
    E result = null;
    if (!resultSet.wasNull())
    {
    try
    {
    for(int i=0; i < theEnumValues.length && result == null; i++)
    {
    if (theEnumValues[i].ordinal() == val)
    {
    result = theEnumValues[i];
    }
    }
    }
    catch (SecurityException e)
    {
    result = null;
    }
    catch (IllegalArgumentException e)
    {
    result = null;
    }
    }
    return result;
    }
    public void nullSafeSet(PreparedStatement preparedStatement,
    Object value, int index) throws HibernateException, SQLException
    {
    if (null == value)
    {
    preparedStatement.setNull(index, Types.SMALLINT);
    }
    else
    {
    preparedStatement.setInt(index, ((Enum)value).ordinal());
    }
    }
    public Object deepCopy(Object value) throws HibernateException
    {
    return value;
    }
    public boolean isMutable()
    {
    return false;
    }
    public Object assemble(Serializable cached, Object owner) throws HibernateException
    {
    return cached;
    }
    public Serializable disassemble(Object value) throws HibernateException
    {
    return (Serializable)value;
    }
    public Object replace(Object original, Object target, Object owner) throws HibernateException
    {
    return original;
    }
    public int hashCode(Object x) throws HibernateException
    {
    return x.hashCode();
    }
    public boolean equals(Object x, Object y) throws HibernateException
    {
    if (x == y)
    return true;
    if (null == x || null == y)
    return false;
    return x.equals(y);
    }
    }
    跟著,新建一個(gè)類來實(shí)現(xiàn)模板
    /** This class is used only in the hibernate XML configuration */
    public class ConsumerTypeEnum extends IntEnumUserType
    {
    public ConsumerTypeEnum()
    {
    // we must give the values of the enum to the parent.
    super(ConsumerType.class,ConsumerType.values());
    }
    }
    最后,在mapping的xml文件中,使用如下代碼
        not-null="true"
    name="consumerType"
    type="ConsumerTypeEnum" />
    注意,這里是使用 ConsumerTypeEnum 而不是 ConsumerType。