Adding an enum as a class property in HBM

前端 未结 6 1914
走了就别回头了
走了就别回头了 2021-01-01 16:38

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

The HBM is similar to this:



        
相关标签:
6条回答
  • 2021-01-01 17:09

    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>
    
    0 讨论(0)
  • 2021-01-01 17:11

    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>
    
    0 讨论(0)
  • 2021-01-01 17:12

    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

    0 讨论(0)
  • 2021-01-01 17:14

    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.

    0 讨论(0)
  • 2021-01-01 17:16

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

    0 讨论(0)
  • 2021-01-01 17:25

    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>
    
    0 讨论(0)
提交回复
热议问题