Adding an enum as a class property in HBM

2019-02-08 03:17发布

问题:

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" />
</class>

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?

回答1:

Here is the solution with Hibernate 3.6.x :

<class name="a.b.c.myObject" table="OBJECT">
  <property name="myEnum" column="EXAMPLE">
    <type name="org.hibernate.type.EnumType">
      <param name="enumClass">a.b.c.myEnum</param>
    </type>       
  </property>
</class>


回答2:

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

@Entity
public class MyObject {
  @Enumerated(EnumType.STRING)
  @Column(name="EXAMPLE")
  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:

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


回答3:

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.



回答4:

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

<class name="a.b.c.myObject" table="OBJECT">
    <property name="myEnum" column="EXAMPLE">
        <type name="org.hibernate.type.EnumType">
            <param name="enumClass">a.b.c.myEnum</param>
            <param name="type">12</param>
        </type>       
    </property>
</class>

12 is equivilant to java.sql.Types.VARCHAR



回答5:

Similar to @monim answer, but more elegant way:

<class name="a.b.c.myObject" table="OBJECT">
    <property name="myEnum" column="EXAMPLE">
        <type name="org.hibernate.type.EnumType">
            <param name="enumClass">a.b.c.myEnum</param>
            <param name="useNamed">true</param>
        </type>       
    </property>
</class>


回答6:

You need to use a UserType to persist that efficiently: https://www.hibernate.org/265.html