Question

I will use the reference tables for the first time so I have some questions about it.

Example Table

------------------------
id | description       |
------------------------
1  | Some description1 |
2  | Some description2 |
3  | Some description3 |
------------------------

1) In order to increase code legibility I want to create enums like example below. Is this best practice to doing this? What do you think?

    public enum JobType {
        SOME_DESC1((short) 1), SOME_DESC2((short) 2), SOME_DESC3((short) 3);

        private Short value;

        JobType(Short value) {
            this.value = value;
        }

        public Short getValue() {
            return value;
        }
    }

2) If I wanted reference tables to be multilingual, how should I do that? Should I create tables like example below? Do you have any other suggestions?

job_type -  job_translation        -   language
   id          job_id    (pk-fk)       lang_code
              lang_code  (pk-fk)       
             description  
Était-ce utile?

La solution

1. enums for reference tables

It depends on what you do with your reference table.

If the reference table is part of your data model, but your code doesn't really need to know what's behind, then don't use such enums. The advantage is that you'd keep flexibility ro add new references without changing the code. Typical example:

  • postal code
  • country prefix
  • category of customers

If your code needs to know the reference because of some specific rules, behaviors, etc. Then such enum could help making the code more readable. Be careful however, because you create a hard coupling between code and data (so maybe call it sys_id instead of id to draw attention on this risk).

2. Multilingual references

Yes, exactly. The usual way is to store the reference text with a language code and an id.

This makes it easy to support as many languages as you want.

Licencié sous: CC-BY-SA avec attribution
scroll top