2012-03-23 15 views
16

Supponiamo che io sono questo enum:Hibernate mappa enum a VARCHAR

public enum TestEnum { EXAMPLE, FURTHER_EXAMPLE, LAST_EXAMPLE } 

Con questa mappatura nel .hbm:

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

L'enum viene inviato al database come 0, 1, 2. Mi piacerebbe che i valori fossero invece memorizzati come EXAMPLE, FURTHER_EXAMPLE o LAST_EXAMPLE in una colonna varchar.

Come si può mappare enum su una colonna varchar?

+1

Possibile duplicato di: http: // StackOverflow. it/questions/1896666/add-an-enum-as-a-class-property-in-hbm – barsju

risposta

14

Aggiungere questo come parametro di EnumType:

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

Questo perché 12 è equivalente a java.sql.Types.VARCHAR

+3

12 è equivalente a java.sql.Types.V Archar. – bvulaj

+2

Ho visto quel valore durante la ricerca di soluzioni, ma ho pensato che fosse un valore predefinito per l'enum. Non è molto descrittivo, sembra un valore magico. Grazie! – ipavlic

3

Se si desidera memorizzare il valore di ogni enum come varchar nel database, si prega di seguire le istruzioni in basso.

  1. Hibernate fornisce un'interfaccia UserTpe. Abbiamo bisogno di creare una classe che implementa l'interfaccia 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. Supponiamo di avere un Enum EncryptionStatus.

    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. Abbiamo bisogno di creare una classe che estende la nostra EnumUserType creato>.

    public class EncryptionStatusType extends EnumUserType<EncryptionStatus>{ 
    
        public EncryptionStatusType() {  
         super(EncryptionStatus.class); 
        } 
    } 
    
  4. Ora abbiamo bisogno di mappare sopra classe creata nel file di hbm.xml al posto di mappatura Enum che immagazzinano valore enum come varchar nel database. Per esempio,

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

15

forse questo è più descrittivo

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

'useNamed' è in ibernazione dal 4.2.0.Indice dove' type' c'è anche 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 /) –

6

È possibile utilizzare le annotazioni come questa:

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

La domanda era per la mappatura hbm, non l'annotazione. – jschreiner