Jesper de Jong wrote:When you use Hibernate, you can tell it how to store it in the database with an annotation. I think that by default it stores the name of the enum constant as a string (so it would store "MALE" or "FEMALE" as a string, if your Person entity contains a Gender member variable). But you can also tell it to store the enum ordinal (as a number). One problem is that if you change the enum (change the names of the constants, or add, remove or put the constants in a different order) you have to be careful that the data in your database might not match the enum anymore.
You use the @Enumerated annotation to tell Hibernate how to map it. For example:
Riaan Nel wrote:My question relates specifically to how one can store an enum value on persisted data. Yes, the gender field could be of type Gender, but if you tried to write that away to SQL (using JDBC for instance), your database will have no idea what the Gender type is.
Ireneusz Kordal wrote:Just store enums as strings.