The status column that changed meaning
A hard Java puzzle in the jpa and hibernate pitfalls family. One line below contains a real bug. Try to spot it first, then read why it fails and how to fix it.
Highlighted: line 9. Play this one as a puzzle without the answer.
What is wrong
ORDINAL stores the position of the constant (0, 1, 2). Inserting REVIEW between IN_PROGRESS and CLOSED shifts the numbers, and every existing CLOSED ticket becomes REVIEW without any error. The column also cannot be understood in a SQL query.
The fix
How to avoid it next time
Store enums as STRING, or use an explicit converter with stable codes.
Want the background? Read JPA entities in the Java concepts library.
About jpa and hibernate pitfalls
The persistence context, lazy loading and entity equality rules create bugs that look fine in a unit test and break once there is a real session, a real transaction or real data volume.
In code review: Ask where the session is open when a lazy field is read, how many queries a loop causes, and whether entity equals and hashCode depend on the generated id.
More puzzles in this family
Frequently asked questions
What is the bug in "The status column that changed meaning"?
ORDINAL stores the position of the constant (0, 1, 2). Inserting REVIEW between IN_PROGRESS and CLOSED shifts the numbers, and every existing CLOSED ticket becomes REVIEW without any error. The column also cannot be understood in a SQL query. The faulty line is line 9: @Enumerated(EnumType.ORDINAL)
How do I fix it?
Replace it with: @Enumerated(EnumType.STRING) Status status;. Store enums as STRING, or use an explicit converter with stable codes.
How do I catch jpa and hibernate pitfalls in code review?
Ask where the session is open when a lazy field is read, how many queries a loop causes, and whether entity equals and hashCode depend on the generated id.
Is there a new puzzle every day?
Yes. The daily Spot the Bug puzzle picks one of these snippets for each UTC day, with the difficulty set by the weekday. Your streak is stored only in your browser.