Hibernate map enum to varchar

ipavlic picture ipavlic · Mar 23, 2012 · Viewed 16k times · Source

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?

Answer

bvulaj picture bvulaj · Mar 23, 2012

Add this as a parameter of EnumType:

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

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