I have an enum type on my Java model which I’d like to map to a table on the database. I’m working with Hibernate Annotations and I don’t know how to do that. Since the answers I search were rather old, I wonder which way is the best?
Thanks in advance
Do you need something else than the
@Enumeratedannotation? For example, the following enum:Could be used and annotated like this:
You can specify how the enum should be persisted in the database with the
EnumTypeenum property of the@Enumeratedannotation.EnumType.ORDINALspecifies that the enum will be persisted as an integer value. Here,myEnumset toVALUE1would be persisted as 0,VALUE2as 1, etc.The alternative is to use
EnumType.STRINGto specify that the enum will be persisted using the name of the enum value that the field is set to. So, applied to the previous example, setting the fieldmyEnumtoMyEnum.VALUE1will persist asVALUE1, etc.