Hibernate @Enumerated seems to be ignored

Nik*_*las 1 java mysql enums hibernate hibernate-5.x

I have the class Person mapped with annotations with enum Sex reffering to the sex if is male or female. Let's see:

@Entity
@Table(name = "PERSON")
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Enumerated(EnumType.STRING)
    @Column(name = "SEX")
    private Sex sex;

    private enum Sex {
        M,
        F;
    } 

    // Getters, setters & constructors
}
Run Code Online (Sandbox Code Playgroud)

When I test getting all the rows from the MySQL database, it works and the mapping is correct.

The database is already predefined, here is the column's definition:

`SEX` enum('M','F') NOT NULL
Run Code Online (Sandbox Code Playgroud)

However the error occurs when I configure Hibernate with hibernate.hbm2ddl.auto=validate:

found [enum (Types#CHAR)], but expecting [varchar(255) (Types#VARCHAR)]
Run Code Online (Sandbox Code Playgroud)

The error is a bit different (expecting [integer (Types#INTEGER)]) happend when I use EnumType.ORDINAL or no @Enumerated at all.

What do I do wrong?

xyz*_*xyz 6

try add columnDefinition

@Enumerated(EnumType.STRING)
@Column(name = "SEX" , columnDefinition="ENUM('M','S')" ,nullable = false )
private Sex sex;
Run Code Online (Sandbox Code Playgroud)

hibernate validate do check types , lenght.... as you have this in db level validator thinks it's different type .

I didn't see it with Oracle , but with MySql it's might be

  • 添加 @Column(columnDefinition = "enum") 也适用于 MySQL (4认同)
  • 简单地添加`@Column(columnDefinition =“ enum”)`似乎可以解决H2问题。 (3认同)
  • 它很冗长,并且在Java代码的两个位置重复了enum声明。以这个价格,我不会在MySQL中使用枚举类型,而是使用varchar,并且会在此列上添加约束以指定接受的值。 (2认同)
  • Postgresql 不喜欢 @Column(columnDefinition = "enum")。有对应的吗? (2认同)