Enum in Hibernate, persisting as an enum

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;

Leave a Comment