adding an enum as a class property in hbm

adding an enum as a class property in hbm  using -'hibernate,enums,hibernate-mapping,hbm'

I am trying to create a class in HBM file which contains an Enum as a field.

The HBM is similar to this:

<class name="a.b.c.myObject" table="OBJECT" >
       <property name="myEnum" column="EXAMPLE" type="a.b.c.myEnum" />

and let's say that this is the Enum:

public enum myEnum{
    a, b, c;

The problem is that in the DB I expected to see the String value of that enum (a,b or c) but instead I got the raw data of that field.

How can I solve that?  

asked Oct 19, 2015 by android_master
0 votes

5 Answers

0 votes

Here is the solution with Hibernate 3.6.x :


answered Oct 19, 2015 by vibhorsingh
0 votes

1) Easy solution: use Hibernate Annotations instead of XML-based mappings. Enum support is built-in:

public class MyObject {
  private MyEnum myEnum;

2) If you can't use annotations, you can still use the EnumType they provide in XML-based mappings. You do need to have appropriate hibernate-annotations.jar in your classpath during deployment but there's no compile-time dependency:


answered Oct 19, 2015 by jekbishnoi
0 votes

For reasons that remain obscure, the Hibernate developers insist on keeping the Hibernate core compatible with pre-Java5, and that means no enum support. When you try to persist an Enum field, it just serializes it, and you get binary data.

If you want to persist enums using an .hbm mapping configuration, you have to create and configure a custom UserType for each enum type you want to handle, which is tedious and irritating. The Hibernate documentation wiki has plenty of examples, many of which seem to contradict each other, and some of which even work.

If you use Hibernate annotations, though, you get full java5 support, including automatic handling of java5 enums. It's just a real shame that you have to do one or the other.

answered Oct 19, 2015 by nimisha.jagtap
0 votes

Just editing @Emmanuel Bourg answer and adding another like this:


12 is equivilant to java.sql.Types.VARCHAR

answered Oct 19, 2015 by deepak
0 votes

Similar to @monim answer, but more elegant way:


answered Oct 19, 2015 by mannar kande