Quick revision
The short answer
ordinal() is the position of a constant in the enum's declaration, so inserting a constant in the middle shifts the numbers of every constant after it, and the integers already stored in the database now point to different constants. We fix it by storing a stable value instead, either name() through @Enumerated(EnumType.STRING) or an explicit code through an AttributeConverter, migrating the existing rows from old ordinals to the new representation, and adding a test that pins the stored code of every constant.
What to remember
In this answer, we saw that ordinal() is derived from declaration order, so inserting PENDING_REVIEW shifted the meaning of every number after it and the stored ordinals mapped to the wrong statuses, while new writes mixed in a second meaning. We fixed the mapping with @Enumerated(EnumType.STRING) or an explicit code through an AttributeConverter, migrated old and new rows separately using the release time, and added a test that pins the stored code of every constant.
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.
ordinal() is the position of a constant in the enum's declaration, so inserting a constant in the middle shifts the numbers of every constant after it, and the integers already stored in the database now point to different constants. We fix it by storing a stable value instead, either name() through @Enumerated(EnumType.STRING) or an explicit code through an AttributeConverter, migrating the existing rows from old ordinals to the new representation, and adding a test that pins the stored code of every constant.
Why this question is asked
This is a bug that passes every code review, because the change looks harmless: someone adds a status in the place that reads best. Then, the morning after the release, suspended customers appear as closed and nobody can explain why. Interviewers like it because the root cause is simple but the fix involves persistence mapping, a careful data migration and a test strategy. In this answer, we will reproduce the problem, see exactly why the data went wrong, fix the mapping, migrate the rows safely and add a test so it cannot happen again.
1A Quick Recap: What ordinal() Is
Every enum constant has an ordinal, its zero-based position in the declaration. The first constant is 0, the next is 1, and so on. The number is not stored anywhere we chose; the compiler derives it from the order of the lines in the source file. That is fine for in-memory use, such as EnumSet and EnumMap, which rebuild everything each time the program starts. It is dangerous for anything that outlives the program, such as a database row.
JPA, the Java persistence standard behind Hibernate, maps an enum field as its ordinal by default. If we write a plain enum field without @Enumerated, or with @Enumerated(EnumType.ORDINAL), the column holds those positions.
2Reproducing the Problem
Here is the original enum and entity, and the data written while it was in production.
| Stored value | Version 1 reads | Version 2 reads |
|---|---|---|
| 0 | ACTIVE | ACTIVE |
| 1 | SUSPENDED | PENDING_REVIEW |
| 2 | CLOSED | SUSPENDED |
| 3 | (did not exist) | CLOSED |
Now a developer adds a new status, PENDING_REVIEW, and puts it after ACTIVE because that is where it fits in the business flow.
3Why the Data Is Now Wrong
After the change the enum reads ACTIVE, PENDING_REVIEW, SUSPENDED, CLOSED, with ordinals 0 to 3. The database still holds 1 for account 2 and 2 for account 3, because nothing touched the rows. When version 2 loads account 2, Hibernate reads 1 and returns PENDING_REVIEW, and account 3, which was closed, now looks suspended.
But there is a catch that makes the incident worse: the new version keeps writing. Every account suspended after the release is stored as 2, and every account closed is stored as 3. So the table now mixes two meanings of the same numbers, and the value 2 means CLOSED for old rows and SUSPENDED for new ones. That is why the migration below needs the release time.
4Fix 1: Store a Stable Value
The simplest stable value is the name. With @Enumerated(EnumType.STRING), Hibernate writes name() and reads with valueOf(), so the order of constants no longer matters. The cost is a slightly wider column and a rule that constants must not be renamed.
When we also want freedom to rename constants, or the column must hold a short code that other systems read, we give each constant an explicit code and map it with an AttributeConverter.
5Fix 2: An Explicit Code With an AttributeConverter
An AttributeConverter is a small JPA class that turns an attribute into a column value and back. Here the code lives on the constant, so the Java name and the stored value are independent.
Notice that the unknown-code case throws a clear exception that names the bad value. A silent default would hide corrupted data.
6Migrating the Existing Rows
Now we must convert the stored numbers into the new representation, and the rows written before and after the bad release mean different things. We need a way to tell them apart, usually an updated_at column or an audit table, and the exact time version 2 went live. If we cannot separate them reliably, the rows written after the release need checking against another source, such as the event log of status changes.
- Roll back or freeze status writesstop new rows with shifted meaning
- Find the release timesplit rows written by v1 and v2
- Add a stable status_code columnmap each group with its own CASE
- Every row mapped and verified?YesDeploy the converter, drop the old columnNoCheck unmapped rows against the event log
- Add the pinning testthe codes can never move silently
5 steps in order. Play them, or step through them one at a time.
We run the migration with a tool such as Flyway or Liquibase, deploy the code that reads status_code, check the counts, and only then drop the old column. While we prepare all this, the first step of the incident response is usually to roll back to version 1, so that no more rows are written with the shifted meaning.
7A Test That Pins the Stored Codes
Finally, we make the mistake impossible to repeat silently. A unit test lists the stored code of every constant. Adding a constant then requires adding a line, and changing an existing code fails the build with a message that explains why.
8Follow-up questions
Is EnumType.STRING always enough?
It is enough as long as nobody renames a constant, because a rename breaks reading old rows exactly like a reorder breaks ordinals. If renames are likely, or the value is shared with other systems, an explicit code with a converter separates the Java name from the stored value.
Could a database check constraint have caught this?
A check constraint limiting the column to 0, 1 and 2 would have rejected the new value 3, which is a useful alarm, but it would not have caught the shifted meaning of 1 and 2. Constraints on string codes are more helpful, and some databases also offer native enum types; Hibernate 6 can map to those on some dialects, so check your version.
Why not just put the new constant at the end?
That would avoid this incident, but it relies on every future developer remembering an invisible rule. Ordering constants for readability is natural, so the safe design is a stored value that does not depend on order at all.
9Conclusion
In this answer, we saw that ordinal() is derived from declaration order, so inserting PENDING_REVIEW shifted the meaning of every number after it and the stored ordinals mapped to the wrong statuses, while new writes mixed in a second meaning. We fixed the mapping with @Enumerated(EnumType.STRING) or an explicit code through an AttributeConverter, migrated old and new rows separately using the release time, and added a test that pins the stored code of every constant.
Open with
The column stores ordinals, which is JPA's default when there is no @Enumerated or it says ORDINAL. An ordinal is just the constant's position in the declaration, so when someone inserted PENDING_REVIEW after ACTIVE, every constant after it moved up by one.
Then, if they want more
The rows did not change, so a stored 1, which meant SUSPENDED, now reads as PENDING_REVIEW, and a stored 2, which meant CLOSED, reads as SUSPENDED. Worse, the new version keeps writing with the new numbering, so after the release the same number means different things in different rows.
To fix it, first I roll back or freeze status writes so the damage stops growing. Then I change the mapping to something stable: @Enumerated with EnumType.STRING, which stores name, or, if we want freedom to rename constants, an explicit code on each constant with an AttributeConverter.
I write a migration that adds a code column and maps rows written before the release with the old ordinal table and rows written after it with the new one, using updated_at or the audit log to split them, and I verify no row is left unmapped before dropping the old column.
Finally I add a unit test that pins the stored code of every constant, so a change like this fails the build.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Hibernate 6
- 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 "A service stores enum ordinal() values in a database column. After someone adds a...": https://scrivio-interview-library.netlify.app/java/enums/76223dd0be/