JPA's Default Enum Storage Can Silently Corrupt Your Database Records
When using JPA's @Enumerated annotation without explicit configuration, it stores enum values as ordinal integers by default, which can corrupt existing data if the enum order is ever changed. For example, inserting a new status mid-list shifts all subsequent ordinals, causing old database rows to map to entirely different enum constants with no errors thrown. Using EnumType.STRING instead stores the constant's name, making reordering safe while only requiring a data migration if a constant is renamed. For stronger guarantees, PostgreSQL's native enum type can enforce validity at the database level, rejecting invalid values regardless of the application layer. Developers using PostgreSQL must also add stringtype=unspecified to their JDBC connection URL to prevent type-casting errors when pairing native enum columns with JPA's STRING mapping.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in