Enum in Hibernate, persisting as an enum

76,533

Solution 1

My understanding is that MySQL enum type is very proprietary and not well supported by Hibernate, see this comment from Gavin King (this related issue is a bit different but that's not the important part).

So, I actually think that you'll have to use your own UsereType and I'd recommend to use the Flexible solution - working version from the Java 5 EnumUserType (see Appfuse's Java 5 Enums Persistence with Hibernate for an example).

Personally, I'd just forget the idea to use MySQL enum, I'm not convinced that the "benefits" are worth it (see this answer for more details).

Solution 2

If you give Hibernate a column definition, it won't try to guess one:

@Column(columnDefinition = "enum('MALE','FEMALE')")
@Enumerated(EnumType.STRING)
private Gender gender;

If you aren't relying on Hibernate to generate your schema for any reason, you don't even have to provide real values for the columnDefinition. This way, you remove an instance where you need to keep the values in sync. Just keep your Java enum and your Liquibase or SQL script in sync:

@Column(columnDefinition = "enum('DUMMY')")
@Enumerated(EnumType.STRING)
private ManyValuedEnum manyValuedEnum;

Solution 3

Try to use @Enumerated(EnumType.STRING) and define your enum like this

enum Gender {
  male,
  female
}

Note lower case values.

This will work at least with VARCHAR columns. It will store enum as string 'male' or 'female'.

Solution 4

not sure why it is not in Hibernate documentation but you can do this

<property name="type" column="type" not-null="true">
    <type name="org.hibernate.type.EnumType">
        <param name="enumClass">com.a.b.MyEnum</param>
        <param name="type">12</param>
        <!-- 12 is java.sql.Types.VARCHAR -->
    </type> 
</property>

Solution 5

Not for this case but if someone is using XML mapping:

<property name="state" column="state" not-null="true">
  <type name="org.hibernate.type.EnumType">
    <param name="enumClass">com.myorg.persistence.data.State</param>
  </type>
</property>

The database column type must be numeric e.g. tinyint on a mysql to make reading ordinal values possible.

Share:
76,533
niklassaers
Author by

niklassaers

Updated on July 09, 2022

Comments

  • niklassaers
    niklassaers almost 2 years

    In my MySQL database, there's the column "gender enum('male','female')"

    I've created my enum "com.mydomain.myapp.enums.Gender", and in my Person entity I'm defined "Gender gender".

    Now I'd want to keep the enum type in my MySQL database, but when I launch my application I get:

    Wrong column type in MyApp.Person for column Gender. Found: enum, expected: integer

    Why is this? This would be the equivalent as if I'd annotated my "Gender gender" with "@Enumerated(EnumType.ORDINAL)", which I haven't. EnumType seems only to be able to be either ORDINAL or STRING, so how do I specify that it should treat the field as an enum, not as an int? (not that there's much difference, but enough for it to get upset about it.)

  • niklassaers
    niklassaers about 14 years
    As written, I'm using enum type column, not varchar
  • Juha Syrjälä
    Juha Syrjälä about 14 years
    Have you actually tried my suggestion? gender type in mysql is probably just some wrapping/syntactic sugar around a varchar or char column.
  • arun
    arun over 9 years
    Thx, this works fine with a MySQL ENUM column, though 12 is for VARCHAR
  • rajadilipkolli
    rajadilipkolli about 7 years
    How to do this with annotations?