Quick revision
The short answer
A native PostgreSQL ENUM is compact and type-safe but awkward to change, a VARCHAR with a CHECK constraint is readable and easy to change with a migration, and a lookup table adds metadata and foreign keys at the cost of a join. @Enumerated(EnumType.ORDINAL) stores the constant's position, so reordering or inserting a constant in the Java enum silently changes the meaning of every stored row; we use EnumType.STRING or a converter with stable codes instead.
What to remember
In this answer, we saw that a native PostgreSQL ENUM is compact and type-safe but needs ALTER TYPE to add values and is awkward to change otherwise, a VARCHAR with CHECK is readable and easy to migrate, and a lookup table adds metadata and foreign keys. EnumType.ORDINAL stores positions, so reordering the Java enum silently corrupts meaning; EnumType.STRING or an AttributeConverter with stable codes avoids it. We choose by how often values change and who else reads the database.
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.
A native PostgreSQL ENUM is compact and type-safe but awkward to change, a VARCHAR with a CHECK constraint is readable and easy to change with a migration, and a lookup table adds metadata and foreign keys at the cost of a join. @Enumerated(EnumType.ORDINAL) stores the constant's position, so reordering or inserting a constant in the Java enum silently changes the meaning of every stored row; we use EnumType.STRING or a converter with stable codes instead.
Why this question is asked
Every system has statuses: an order is NEW, PAID, SHIPPED or CANCELLED. How we store them decides how painful it is to add a status next year, whether a report written by another team can read them, and whether a harmless-looking change in Java can corrupt data. Interviewers ask this to see whether we think about change over time and about who else reads the database. In this answer, we will compare the three database options with code, look at the ORDINAL trap and its fixes, see how Hibernate maps native enums, and finish with a way to choose.
1Option 1: A Native PostgreSQL ENUM Type
PostgreSQL lets us define a type with a fixed list of labels. A column of that type accepts only those labels, and each value takes 4 bytes on disk. The labels also sort in the order they were declared, not alphabetically, which can be handy for statuses that progress.
So adding is easy, renaming is possible, but removing a value or changing the order needs a new type and a rewrite of every column that uses it. There is also a catch with ADD VALUE: the new label cannot be used in the same transaction that added it, which matters for migration tools that wrap everything in one transaction.
2Option 2: VARCHAR With a CHECK Constraint
Here the column is ordinary text, and a CHECK constraint lists the allowed values. Anyone reading the table sees plain words, and changing the list is a normal migration that drops and recreates the constraint.
The text costs a few more bytes per row than an enum, which rarely matters for a status column. Adding the constraint as NOT VALID and validating it separately avoids holding a strong lock while PostgreSQL scans a large table.
3Option 3: A Lookup Table
A lookup table, also called a reference table, stores the statuses as rows, and the orders table points to them with a foreign key. Now each status can carry metadata: a display label, a sort order, whether it is still active, or whether it counts as a final state.
| Option | Add a value | Remove or rename | Metadata | Readable by others |
|---|---|---|---|---|
| Native ENUM | ALTER TYPE ADD VALUE | rename yes, remove needs new type | no | yes |
| VARCHAR + CHECK | replace the constraint | migration updates rows | no | yes |
| Lookup table | insert a row | deactivate the row | yes | yes, with a join |
| ORDINAL integer | risky, see below | corrupts meaning | no | no, just 0, 1, 2 |
Retiring a status becomes setting active = false, so old orders keep their history while new code stops using it. Reports can join to get labels without hard-coding them. The cost is a join for display and a little more ceremony for every new value.
4The ORDINAL Trap in JPA
In JPA, @Enumerated decides how a Java enum is written to the column. EnumType.ORDINAL writes the constant's position in the enum declaration, starting at 0. That is compact, but the meaning of each number now depends on the order of lines in a Java file.
Nothing fails. No exception, no migration warning, just wrong data. That is why ORDINAL is dangerous. The usual fix is EnumType.STRING, which stores the constant's name, so reordering is harmless; only renaming a constant needs a data migration. When names may change or the database needs short stable codes, an AttributeConverter maps each constant to a code we control.
5Fix: STRING or a Converter With Stable Codes
Both fixes are short. The converter version keeps the code inside the enum, so renaming the Java constant does not touch stored data.
To map a Java enum to a native PostgreSQL enum column, Hibernate 6 needs to know the column has a named enum type. In Hibernate 6.2 and later this is done with @JdbcTypeCode(SqlTypes.NAMED_ENUM) on an @Enumerated(EnumType.STRING) field; the exact annotation has moved between versions, so check your version.
6How to Choose
The choice depends on two questions. How often do the values change? And who else reads this database? A status set that is stable for years and read only by our service fits a native enum or a CHECK constraint. A set that changes every few months fits VARCHAR with CHECK, because each change is a plain migration. When the values carry behaviour or display data, when other systems or BI tools read the table, or when statuses must be retired without breaking history, the lookup table wins. And in every case, the Java side uses STRING or a converter, never ORDINAL.
7Follow-up questions
Is the native enum really faster than varchar?
It is smaller, 4 bytes against a few bytes plus a header, so indexes are slightly smaller. For a status column the difference is rarely measurable. We choose on how easy changes are, not on speed.
Can a lookup table use an integer key instead of a code?
It can, and it saves a few bytes, but then raw queries on orders show numbers again, much like ORDINAL. A short readable code as the key keeps data understandable without a join, which is why many teams prefer it.
How do we fix a database that already uses ORDINAL?
Add a new text column, fill it with a CASE expression that maps each number to the name using the enum order from the version that wrote the data, switch the mapping to STRING on the new column, and drop the old one. Check history first: if the enum was ever reordered, some rows may already be wrong.
8Conclusion
In this answer, we saw that a native PostgreSQL ENUM is compact and type-safe but needs ALTER TYPE to add values and is awkward to change otherwise, a VARCHAR with CHECK is readable and easy to migrate, and a lookup table adds metadata and foreign keys. EnumType.ORDINAL stores positions, so reordering the Java enum silently corrupts meaning; EnumType.STRING or an AttributeConverter with stable codes avoids it. We choose by how often values change and who else reads the database.
Open with
There are three common options. A native PostgreSQL ENUM is compact, 4 bytes, and rejects invalid values, but adding a value needs ALTER TYPE, removing one means creating a new type and converting the column, and ADD VALUE has transaction restrictions in migrations.
Then, if they want more
A VARCHAR with a CHECK constraint is readable to anyone and easy to change: a migration replaces the constraint, ideally as NOT VALID followed by VALIDATE on big tables.
A lookup table with a foreign key is the most flexible, because each status can carry a label, sort order and an active flag, so we can retire values without breaking history, at the cost of a join. On the Java side, I never use EnumType.ORDINAL.
It stores the position of the constant, so if someone inserts ON_HOLD between PAID and SHIPPED, every stored 2 suddenly means ON_HOLD, with no error. I use EnumType.STRING, or an AttributeConverter that maps constants to stable codes.
Hibernate 6 can also map to a native PostgreSQL enum with a JDBC type code annotation, though the exact form depends on the version. My choice depends on how often the values change and whether other systems read the database: CHECK for most cases, a lookup table when there is metadata or many readers.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Hibernate 6, PostgreSQL 10
- 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
- PostgreSQL documentation, MySQL reference manual
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 "How would you model an order status enum in the database: a native PostgreSQL ENUM type...": https://scrivio-interview-library.netlify.app/databases/data-modeling/f8c347e05b/