Quick revision
The short answer
We would persist a stable external code for every enum, never an ordinal, and accept names only where renames are forbidden; keep that code separate from the Java constant name so code can be refactored without migrating data; require every consumer to tolerate unknown values, because producers and stored data outlive any deployment; never remove a value that may exist in stored data, only deprecate it; and build these rules into the shared persistence and serialization libraries, with tests that pin each constant's code.
What to remember
In this answer, we looked at how persisting ordinals, names and ad hoc codes each caused incidents, and set five rules: persist a stable external code and never an ordinal, keep that code separate from the Java name, make every consumer tolerate unknown values, never remove a value that may exist in stored data but deprecate it and never reuse its code, and build all of this into shared persistence and serialization libraries with pinning and architecture tests so that following the rules is the default.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We would persist a stable external code for every enum, never an ordinal, and accept names only where renames are forbidden; keep that code separate from the Java constant name so code can be refactored without migrating data; require every consumer to tolerate unknown values, because producers and stored data outlive any deployment; never remove a value that may exist in stored data, only deprecate it; and build these rules into the shared persistence and serialization libraries, with tests that pin each constant's code.
Background to this question
After years of operation, a company's enums have been written into database columns, caches, message topics and other teams' systems in every possible form. Each incident got its own fix, but the next one came from a different direction. A senior engineer's job here is to replace incident-by-incident fixes with a few rules that make the whole class of failure rare, and to make those rules the easy path for teams. In this answer, we will look at the three ways enums have been persisted and how each failed, derive the rules, and then show how to build them into shared libraries and tests so teams follow them without thinking.
1Three Ways Enums Were Persisted, and How Each Failed
Looking back over the incidents, each one came from one of three representations. Ordinals, the position of a constant in its declaration, were used because they are compact and are JPA's default; they failed when someone inserted a constant in the middle and every stored number after it changed meaning. Names were used because they are readable; they failed when someone renamed a constant in an IDE and every stored copy of the old name became unreadable. Custom codes were used in some places; they mostly worked, but each team wrote its own mapping, some lookups returned null for unknown codes, and some codes were reused for a different meaning after a value was retired.
| Stored as | Breaks when | Typical incident |
|---|---|---|
ordinal() | a constant is inserted or reordered | suspended accounts read as closed |
name() | a constant is renamed | old sessions and messages unreadable |
| Ad hoc codes | unknown codes, reused codes | null status, wrong meaning for old rows |
| Stable code, central mapping | only if a code is changed or reused | rare, and caught by tests |
2Rule 1: Persist a Stable External Code, Never an Ordinal
Every enum value that leaves the process gets an external code that is fixed for ever. Ordinals are banned for persistence and messaging outright, because their meaning depends on source order that developers change for readability. Names are acceptable only where a rename is explicitly forbidden, for example a small internal table, and that restriction is documented next to the enum.
The code is defined on the constant, not derived from it, so it is visible in the source and reviewable.
Notice the deprecated constant. That is the fourth rule in action, and we will come back to it.
3Rule 2: Separate the External Code From the Java Name
Once the stored value is an explicit code, the Java name is free to change. A developer can rename SUSPENDED to FROZEN, or reorder constants for readability, and no data migration is needed, because the code S still maps to the same constant. This is what turns a dangerous refactoring into a safe one.
It also lets the code follow the needs of other systems. A partner may require numeric codes, a reporting team may prefer short letters, and the Java name can still read naturally. The mapping is in one place, the constant's declaration, rather than spread across converters and SQL scripts.
4Rule 3: Consumers Tolerate Unknown Values
Producers and stored data outlive any single deployment. A message produced by a newer service version can reach an older consumer; a row written by tomorrow's release can be read by today's batch job during a rollback. So every reader must handle a code it does not recognize without crashing or blocking: map it to an explicit UNKNOWN constant, log and count it, and handle UNKNOWN deliberately in code. The value read is kept as raw text where possible, so it can be written back unchanged and nothing is lost when an old version passes the data along.
5Rule 4: Never Remove a Value That May Exist in Stored Data
Retiring a value is the change teams most often get wrong. If we delete the constant PENDING_KYC, every old row or retained message that holds K becomes unreadable, or worse, K is later reused for a new meaning and old rows silently change meaning. So we deprecate instead: the constant stays, producers stop writing it, a check rejects new writes, readers still understand it, and its code is never reused. Only after a data migration has rewritten every stored copy, and retention has expired for every message, can it be removed, and even then the code stays reserved.
6Making the Rules the Easy Path
Rules that rely on memory fail across many teams and many years. So we put them into the shared libraries that teams already use. A PersistentEnum interface with a code() method, a generic JPA AttributeConverter and Jackson serializer and deserializer that work for any such enum, and a lookup that maps unknown codes to UNKNOWN with a metric. Teams then get correct persistence by implementing one interface.
- Add constant with a new, never-used codepinning test updated
- Readers deploy firstthey understand the code before anyone writes it
- Writers start using itbehind a flag if the meaning matters
- Retire: mark @Deprecatedno new writes, still readable
- Migrate stored copies, wait out retentionthe code stays reserved for ever
5 steps in order. Play them, or step through them one at a time.
Alongside the library, each persistent enum gets a pinning test that lists every constant with its code, so changing or reusing a code fails the build with a message explaining why. An architecture test bans @Enumerated(EnumType.ORDINAL) and enums without codes in entity and message classes. With those in place, the rules hold even for teams that have never read them.
7Follow-up questions
Isn't a code per constant extra work for every enum?
It is one string per constant, written once. Compared with a data migration after a rename or an incident from shifted ordinals, it is very cheap, and the shared converter means no extra mapping code.
How would you migrate existing ordinal and name columns?
One enum at a time: add codes to the enum, add a new column, backfill it with a mapping from the old representation, deploy readers that use the new column, then stop writing the old one and drop it. The pinning test goes in with the first step.
What about enums that only live in memory?
They are unaffected. The rules apply only to values that leave the process: databases, caches, messages, files and APIs. EnumSet, EnumMap and ordinals in memory are fine.
8Conclusion
In this answer, we looked at how persisting ordinals, names and ad hoc codes each caused incidents, and set five rules: persist a stable external code and never an ordinal, keep that code separate from the Java name, make every consumer tolerate unknown values, never remove a value that may exist in stored data but deprecate it and never reuse its code, and build all of this into shared persistence and serialization libraries with pinning and architecture tests so that following the rules is the default.
Open with
Looking at the incidents, each one came from the representation: ordinals broke when someone inserted a constant, names broke on a rename, and ad hoc codes broke on unknown or reused codes.
Then, if they want more
So I would set a few rules. First, anything that leaves the process stores a stable external code defined on the constant, never an ordinal, and names only where renames are explicitly forbidden. Second, the code is separate from the Java name, so developers can rename or reorder constants without any data migration.
Third, consumers must tolerate unknown codes, mapping them to an explicit UNKNOWN with a metric, because producers and stored data outlive any one deployment and rollbacks happen. Fourth, we never delete a value that might exist in stored data or retained messages: we deprecate it, stop writing it, keep reading it, and never reuse its code.
And fifth, I would not rely on people remembering this. I would put it into the shared libraries: a PersistentEnum interface with a code method, a generic JPA converter and Jackson serializers that use it, a lookup with the unknown-value policy built in, a pinning test per enum, and an architecture test that bans ordinal mapping in entities and messages. Then the correct path is also the easiest one.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Across years of operation, enum values have been persisted as ordinals, names and custom...": https://scrivio-interview-library.netlify.app/java/enums/ae8f78d8f1/