Adding an enum as a class property in HBM

2019-02-08 03:30发布

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?

6条回答
我命由我不由天
2楼-- · 2019-02-08 03:48

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>
查看更多
欢心
3楼-- · 2019-02-08 03:48

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>
查看更多
Anthone
4楼-- · 2019-02-08 03:49

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.

查看更多
够拽才男人
5楼-- · 2019-02-08 03:49

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

查看更多
一夜七次
6楼-- · 2019-02-08 04:01

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

查看更多
Deceive 欺骗
7楼-- · 2019-02-08 04:04

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>
查看更多
登录 后发表回答