Is there a “canonical” way to represent a dictionary in – maintained through java persistence – database?
Lets say I have a table of people and there is a column “profession”.
Set of professions is restricted but can be extended. Some professions have some special meanings for a system, like a military, or a doctor.
- I can use enum for professions and store string (name() method) values in database as it shown here. It is simple and readable.
- In database I can have a dictionary table ‘profession’ with professions (id, name_of_profession), and table ‘people’ which has foreign key (id_profession) from table ‘profession’. Than enum will have an Integer id value that is mapped to id column in ‘profession’ table.
First solution is short and easy. But in that case, database without application has no integrity. Is the second “legacy” way inappropriate?
I think you have identified the pros and cons of the two approaches. It is really up to you to decide which is better … for your specific application.
Or to put it another way, “best practice” depends on your application’s real requirements.