简体   繁体   中英

Hibernate enum casting and converter mapping

I' m working with enums and I want to store code from enum body in database. I' m using hibernate 5.2.11.. For the entity mapping I'm using .hbm.xml files. What I'm doing wrong? Why I can't store the enmus code. It keeps storing enum name or bytearray.

MyEntity.java

    public class MyEntity {

       private Long id;
       private MyEnums num;

       public Long getId() {
           return id;
       }

       public void setId(Long id) {
           this.id = id;
       }

       @Convert(converter = MyEnumConverter.class)
       public MyEnums getNum() {
           return num;
       }

       @Convert(converter = MyEnumConverter.class)
       public void setNum(MyEnums num) {
           this.num = num;
       }
    }

Using this entity I'm setting enum and I want that hibernate store the code from the enum body. MyEnums.java

    public enum MyEnums {

       FIRST("123", "first"),
       SECOND("456", "second");

       private final String code;
       private final String description;

       private MyEnums(String code, String description) {
           this.code = code;
           this.description = description;
       }

       public static MyEnums fromCode(String code) {
           if (code.equals(FIRST.code)) {
               return FIRST;
           } else if (code.equals(SECOND.code)) {
               return SECOND;
           } else {
               return null;
           }
       }

       public String getCode() {
           return code;
       }

       public String getDescription() {
           return description;
       }
    }

Using MyEnums class logic I can get code or description from enum body. To store code into database I am using AttributeConverter<> .

MyEnumConverter.java

    @Converter(autoApply = true)
    public class MyEnumConverter implements AttributeConverter<MyEnums, String> {

       @Override
       public String convertToDatabaseColumn(MyEnums myEnums) {
           return (myEnums != null)? myEnums.getCode() : null;
       }

       @Override
       public MyEnums convertToEntityAttribute(String code) {
           return (code != null)? MyEnums.fromCode(code) : null;
       }
    }

As I said I'm using .hbm.xml mapping. So I tried two different ways to map my enum type. When I'm using 1-ST annotation - database store bytea type value. And when I'm using 2-nd annotation - database stores enum name or possition number in enum class.

MyEntity.hbm.xml

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

    <hibernate-mapping>
        <class name="main.test.main.MyEntity" table="test_enums">
            <id name="id" column="id">
                <generator class="org.hibernate.id.enhanced.SequenceStyleGenerator">
                    <param name="optimizer">none</param>
                    <param name="increment_size">1</param>
                    <param name="sequence_name">seq_enums</param>
                </generator>
            </id>

       <!-- 1-ST ANNOTATION -->
            <property name="num" column="enum_code"/>

       <!-- 2-ND ANNOTATION -->
            <!--<property name="num" column="enum_code">
                <type name="org.hibernate.type.EnumType">
                    <param name="enumClass">main.test.main.MyEnums</param>
                </type>
            </property>-->
        </class>
    </hibernate-mapping>

When I' m using 1-st annotation database table looks like this:

----------------------------------
| id | enum_code                 |
----------------------------------
| 1  | 81B 00000000 AC ED 00 ... |
----------------------------------

When I' m using 2-nd annotation table looks like this:

----------------------------------
| id | enum_code                 |
----------------------------------
| 1  | SECOND                    |
----------------------------------

Or like this:

----------------------------------
| id | enum_code                 |
----------------------------------
| 1  | 1                         |
----------------------------------

So seems that my enum converter does not work. Is someone encountered with this type of problem? I really need some help...

I think that hibernate is considering the field and ignoring getter/setter, try to annotate the field ...

public class MyEntity {

   private Long id;
   @Convert(converter = MyEnumConverter.class)
   private MyEnums num;

   public Long getId() {
       return id;
   }

   public void setId(Long id) {
       this.id = id;
   }

   public MyEnums getNum() {
       return num;
   }

   public void setNum(MyEnums num) {
       this.num = num;
   }
}

I think the problem comes from the use of autoApply = true in the @Converter in your Converter, it will be always applied.

Instead you can use Enumerated with your field, with two options:

EnumType.ORDINAL :

<property name="num" column="enum_code">
    <type name="org.hibernate.type.EnumType">
        <param name="enumClass">main.test.main.MyEnums</param>
    </type>
</property>

EnumType.STRING

<property name="num" column="enum_code">
    <type name="org.hibernate.type.EnumType">
        <param name="enumClass">main.test.main.MyEnums</param>
        <param name="useNamed">true</param>
    </type>
</property>

Fo further details you can check Hibernate Enum Type Mapping Example .

After long time of Internet browsing finally I found the solution . Thanks for Gabriel Axel. He is offering to create your own value type class which converts enum to his body value.

More information (example): http://www.gabiaxel.com/2011/01/better-enum-mapping-with-hibernate.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM