"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
Kevin Tysen wrote:
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
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:
"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
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.
Whip out those weird instruments of science and probe away! I think it's a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|