Scrivio Interview library
DebuggingFoundations7 min read · updated 12 November 2026

A service stores enum ordinal() values in a database column. After someone adds a constant in the middle of the enum, existing data maps to wrong statuses. Explain and fix.

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.

Java
enum AccountStatus { ACTIVE, SUSPENDED, CLOSED } // ordinals 0, 1, 2
@Entity
class Account {
@Id Long id;
AccountStatus status; // no @Enumerated: stored as ordinal
}
-- rows written by version 1
-- id | status
-- 1 | 0 ACTIVE
-- 2 | 1 SUSPENDED
-- 3 | 2 CLOSED
The same stored numbers, read by two versions
Stored valueVersion 1 readsVersion 2 reads
0ACTIVEACTIVE
1SUSPENDEDPENDING_REVIEW
2CLOSEDSUSPENDED
3(did not exist)CLOSED
Nothing in the table changed; only the meaning of each number did.

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.

Java
enum AccountStatus { ACTIVE, PENDING_REVIEW, SUSPENDED, CLOSED }
Account a = em.find(Account.class, 2L);
System.out.println(a.status); // PENDING_REVIEW: it was SUSPENDED
Account b = em.find(Account.class, 3L);
System.out.println(b.status); // SUSPENDED: it was CLOSED

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.

Java
@Enumerated(EnumType.STRING)
@Column(length = 32)
AccountStatus status; // stored as the text SUSPENDED

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.

Java
enum AccountStatus {
ACTIVE("A"), PENDING_REVIEW("P"), SUSPENDED("S"), CLOSED("C");
final String code;
AccountStatus(String code) { this.code = code; }
static AccountStatus fromCode(String c) {
for (AccountStatus s : values()) if (s.code.equals(c)) return s;
throw new IllegalArgumentException("Unknown account status code: " + c);
}
}
@Converter(autoApply = true)
class AccountStatusConverter implements AttributeConverter<AccountStatus, String> {
public String convertToDatabaseColumn(AccountStatus s) { return s == null ? null : s.code; }
public AccountStatus convertToEntityAttribute(String c) { return c == null ? null : AccountStatus.fromCode(c); }
}
AccountStatus.SUSPENDED.code; // "S", no matter where SUSPENDED is declared

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.

SQL
ALTER TABLE account ADD COLUMN status_code VARCHAR(1);
-- rows last written by version 1 (old ordinals)
UPDATE account SET status_code = CASE status WHEN 0 THEN 'A' WHEN 1 THEN 'S' WHEN 2 THEN 'C' END
WHERE updated_at < TIMESTAMP '2026-10-20 06:00:00';
-- rows last written by version 2 (new ordinals)
UPDATE account SET status_code = CASE status WHEN 0 THEN 'A' WHEN 1 THEN 'P' WHEN 2 THEN 'S' WHEN 3 THEN 'C' END
WHERE updated_at >= TIMESTAMP '2026-10-20 06:00:00';
SELECT count(*) FROM account WHERE status_code IS NULL; -- 0 before we drop the old column
Step throughRecovering from the shifted ordinals
  1. Roll back or freeze status writesstop new rows with shifted meaning
  2. Find the release timesplit rows written by v1 and v2
  3. Add a stable status_code columnmap each group with its own CASE
  4. Every row mapped and verified?
    YesDeploy the converter, drop the old columnNoCheck unmapped rows against the event log
  5. 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.

Java
@Test
void storedCodesNeverChange() {
Map<AccountStatus, String> pinned = Map.of(
AccountStatus.ACTIVE, "A", AccountStatus.PENDING_REVIEW, "P",
AccountStatus.SUSPENDED, "S", AccountStatus.CLOSED, "C");
assertEquals(EnumSet.allOf(AccountStatus.class), pinned.keySet(), "pin the code of every new constant");
pinned.forEach((s, code) -> assertEquals(code, s.code, "stored codes are data; never change them"));
}

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.

What to say in the interviewAbout 80 seconds aloud

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.
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/