How to handle duplication between Java enumerations and database tables?

In our application, it is common that some entities must be represented by enumerations: for example, types, categories, states and similar things

Usually, there are conditions or processes in the code that use values to determine one action or another, so the value must be "known" to the application in some way (that is, it must be able to refer to specific instances rather than to classes as a whole) This is why we use enumerations instead of ordinary classes

The problem is that these entities also need to store (or at least be referenced) fields in the database as other entities We usually create a table for each entity so that we can check the reference integrity in these columns, and only the data in the database has "meaning", instead of referring to enumeration to find the meaning of each ID

Ideally, the data of these entities should be populated from the data in the enumeration, but now we repeat these values in the DB initialization script

When using ORM like hibernate, it becomes a little complicated

I want to know how others deal with this situation

I'm not sure about the idea of repeating between enumerations and database tables, but I haven't found a better solution yet

Solution

Try the answer to this question: ways to save enums in database

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>