I need to create an enum based on a table from the database.
DB table MyColors: id/title/value 1/Red/1 2/Green/4
dynamic create
enum MyColors {
Red=1,
Green=4;
}
You can dynamically create source code by reading from the database and simply outputting the results in a format conducive to building an enum. However, it is impractical to create an enum at run time. You would be better off with some kind of associative array.
Actually there is a possibility of dynamically creating enums using reflection: http://niceideas.ch/roller2/badtrash/entry/java_create_enum_instances_dynamically
It's not clear if you want to generate source code or not. I guess not, since even compiled no code in the same program could access the the enum objects except through reflection.
So why not mapping the table to a ColorEntity object using JPA? You can then have a list or a map of these entities or whatever you need.
1isn't assignable toMyColors.Redenumcan't be created dynamically. stackoverflow.com/questions/857414