22

Suppose I have this enum:

public enum TestEnum { EXAMPLE, FURTHER_EXAMPLE, LAST_EXAMPLE }

With this mapping in the .hbm:

<property name="testEnum" column="TEST_COLUMN">
    <type name="org.hibernate.type.EnumType">
        <param name="enumClass">p.a.c.k.TestEnum</param>
    </type>
 </property>

The enum is sent to the database as 0, 1, 2. I'd like the values to be instead stored as EXAMPLE, FURTHER_EXAMPLE or LAST_EXAMPLE in a varchar column.

How can I map enum to a varchar column?

ipavlic
  • 4,906
  • 10
  • 40
  • 77
  • 1
    Possible duplicate of: http://stackoverflow.com/questions/1896666/adding-an-enum-as-a-class-property-in-hbm – barsju Mar 23 '12 at 12:57

4 Answers4

18

Add this as a parameter of EnumType:

<param name="type">12</param>

This is because 12 is equivalent to java.sql.Types.VARCHAR

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
bvulaj
  • 5,023
  • 5
  • 31
  • 45
  • 4
    12 is equivalent to java.sql.Types.VARCHAR. – bvulaj Mar 23 '12 at 13:03
  • 2
    I saw that value while browsing for solutions, but thought it was a default value for the enum. It's not very descriptive, looks like a magic value. Thank you! – ipavlic Mar 23 '12 at 13:13
16

Maybe this is more descriptive

<param name="useNamed">true</param>
peter
  • 161
  • 1
  • 2
  • `useNamed` is there in hibernate since 4.2.0.Final where as `type` is there even in 3.1beta9 [link](http://grepcode.com/file/repo1.maven.org/maven2/org.hibernate/hibernate-core/4.2.0.Final/org/hibernate/type/EnumType.java/) [link](http://grepcode.com/file/repo1.maven.org/maven2/org.hibernate/hibernate-annotations/3.1beta9/org/hibernate/type/EnumType.java/) – Abhishek Oza Jan 16 '17 at 11:30
5

You can use annotations like this:

public class MyClass {
    TestEnum testEnum;
    @column(name="TEST_COLUMN")
    @Enumerated(EnumType.STRING)
    public TestEnum getTestEnum(){
        this.testEnum;
    }
}
monim
  • 3,427
  • 3
  • 23
  • 36
3

If you want to store any enum's value as varchar in database, please follow below steps.

  1. Hibernate provides an UserTpe interface. We need to create a class which implements UserType interface.

    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. Suppose I have a 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. We need to create a class which extends our created EnumUserType>.

    public class EncryptionStatusType extends EnumUserType<EncryptionStatus>{
    
       public  EncryptionStatusType() {     
           super(EncryptionStatus.class);
       }
    }
    
  4. Now we need to map above created class in hbm.xml file in stead of Enum mapping which store enum value as varchar in the database. For Example,

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

black666
  • 2,997
  • 7
  • 25
  • 40
kandarp
  • 4,979
  • 11
  • 34
  • 43