18

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;
}
6
  • 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: stackoverflow.com/questions/492096/… Commented Feb 3, 2011 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 Commented Feb 3, 2011 at 19:03
  • What you want created doesn't compile, 1 isn't assignable to MyColors.Red Commented Feb 3, 2011 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? Commented Feb 3, 2011 at 19:05
  • Duplicates, this comes up all the time. Unfortunately Java's enum can't be created dynamically. stackoverflow.com/questions/857414 Commented Feb 3, 2011 at 19:22

4 Answers 4

13

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 is using to handle database metadata.
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.
6

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

1 Comment

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.
2

One option 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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.