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?
Maybe this is more descriptive
Add this as a parameter of EnumType:
This is because
12
is equivalent to java.sql.Types.VARCHARIf you want to store any enum's value as varchar in database, please follow below steps.
Hibernate provides an UserTpe interface. We need to create a class which implements UserType interface.
Suppose I have a EncryptionStatus Enum.
We need to create a class which extends our created EnumUserType>.
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" />
You can use annotations like this: