17

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;
}
Mediator
  • 14,951
  • 35
  • 113
  • 191
  • 5
    That's somewhat of a contradictory question. Enums are usually used for instances that you know exist before runtime. However, I understand your situation. I've asked something in a similar vein: http://stackoverflow.com/questions/492096/persisting-data-suited-for-enums – Rob Hruska Feb 03 '11 at 19:02
  • what exactly are you trying to do? -- if you are trying to replace 1 with red, 4 with green in your sql result, you can do a join to display the corresponding name – Foo Bah Feb 03 '11 at 19:03
  • What you want created doesn't compile, `1` isn't assignable to `MyColors.Red` – meriton Feb 03 '11 at 19:04
  • 1
    Is the goal to create an enum object at run-time or spit out an enum definition (text) that can later be used in code? –  Feb 03 '11 at 19:05
  • Duplicates, this comes up all the time. Unfortunately Java's `enum` can't be created dynamically. http://stackoverflow.com/questions/857414 – Steve Kuo Feb 03 '11 at 19:22
  • What's the use case of a dynamic enum? It sounds like you might be better served by a Map of some sort. – GriffeyDog Feb 03 '11 at 19:29

4 Answers4

14

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.

TheBuzzSaw
  • 8,648
  • 5
  • 39
  • 58
  • 1
    In a company where I used to work, we followed this road and it was feasible until we hit a limit (in the Java compiler or language definition, I don't remember) on the maximum number of constants for a class so they had to split up the source generation into smaller files. It's an extreme case but the general principle is better than using a Map with regard to type soundness and IDE auto-completion. This is the same approach [jOOQ](https://www.jooq.org/#a=usp-code-generation) is using to handle database metadata. – polaretto Aug 21 '18 at 14:08
  • Agreed. Even long before that limit is hit, there simply comes a point where the values being represented as source code constants loses all value. Having a runtime lookup is simply more sane. – TheBuzzSaw Aug 22 '18 at 03:13
6

Actually there is a possibility of dynamically creating enums using reflection: http://niceideas.ch/roller2/badtrash/entry/java_create_enum_instances_dynamically

polaretto
  • 791
  • 9
  • 11
  • 1
    I like that idea. I have the same problem. An enum used everywhere in a very large project which we now suddenly want to be database driven... Refactoring all that code makes me shudder, whereas dynamically building an enum sounds like it would be orders of magnitude simpler. – Brian Knoblauch Jul 19 '16 at 17:12
2

One option to is to define an XML Schema and the required values as enum and generate the class files, so that we can manage the values outside the source code, however we cannot dynamically generate the enum values from the database.

dosakiller
  • 79
  • 5
1

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.

Puce
  • 37,247
  • 13
  • 80
  • 152