Hibernate映射枚举到varchar

时间:2012-03-23 12:51:59

标签: hibernate enums hibernate-mapping varchar

假设我有这个枚举:

public enum TestEnum { EXAMPLE, FURTHER_EXAMPLE, LAST_EXAMPLE }

.hbm

中使用此映射
<property name="testEnum" column="TEST_COLUMN">
    <type name="org.hibernate.type.EnumType">
        <param name="enumClass">p.a.c.k.TestEnum</param>
    </type>
 </property>

枚举以012的形式发送到数据库。我希望将值存储为varchar列中的EXAMPLEFURTHER_EXAMPLELAST_EXAMPLE

如何将枚举映射到varchar列?

4 个答案:

答案 0 :(得分:15)

将此项添加为EnumType:

的参数
<param name="type">12</param>

这是因为12相当于java.sql.Types.VARCHAR

答案 1 :(得分:15)

也许这更具描述性

<param name="useNamed">true</param>

答案 2 :(得分:6)

你可以使用这样的注释:

public class MyClass {
    TestEnum testEnum;
    @column(name="TEST_COLUMN")
    @Enumerated(EnumType.STRING)
    public TestEnum getTestEnum(){
        this.testEnum;
    }
}

答案 3 :(得分:3)

如果要将任何枚举值作为varchar存储在数据库中,请按照以下步骤操作。

  1. Hibernate提供UserTpe界面。我们需要创建一个实现UserType接口的类。

    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 EnumUserType<E extends Enum<E>> implements UserType {
       private Class<E> clazz = null;
    
       protected EnumUserType(Class<E> c) {
           this.clazz = c;
       }
    
       private static final int[] SQL_TYPES = { Types.VARCHAR };
    
       public int[] sqlTypes() {
               return SQL_TYPES;
       }
    
       public Class returnedClass() {
           return clazz;
       }
    
       public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
            throws HibernateException, SQLException {
    
           String name = resultSet.getString(names[0]);
    
           E result = null;
           if (!resultSet.wasNull()) {
               result = Enum.valueOf(clazz, name);
    
           }
           return result;
       }
    
       public void nullSafeSet(PreparedStatement preparedStatement, Object value,
            int index) throws HibernateException, SQLException {
    
           if (null == value) {
               preparedStatement.setNull(index, Types.VARCHAR);
           } else {
               preparedStatement.setString(index, ((Enum) value).name());
           }
       }
    
       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);
       }
    }
    
  2. 假设我有一个EncryptionStatus Enum。

    import java.io.Serializable;
    import com.google.gwt.user.client.rpc.IsSerializable;
    
    public enum EncryptionStatus implements IsSerializable, Serializable {
        PLAIN, HASH, RE_HASH, SUPER_HASH, SUPER_REHASH, OPEN, ENCRYPT, RE_ENCRYPT
    }
    
  3. 我们需要创建一个扩展我们创建的EnumUserType&gt;。

    的类
    public class EncryptionStatusType extends EnumUserType<EncryptionStatus>{
    
       public  EncryptionStatusType() {     
           super(EncryptionStatus.class);
       }
    }
    
  4. 现在我们需要在hbm.xml文件中映射上面创建的类,而不是枚举映射,它将枚举值存储为数据库中的varchar。例如,

  5. <property name="secureStatus" type="com.nextenders.facadeimplementation.util.userenum.EncryptionStatusType" column="secure_status" />