Can I apply @Enumerated(EnumType.STRING) once globally instead of on each enum?

Viewed 110

I want all enums in my classes persisted by JPA as Strings, not ordinal. Can I declare that globally someplace? I'm using Spring Data JPA + Hibernate + postgresql.

1 Answers

The answer is no.

Enum parameters are a compile time constants - there is no avenue to change them at runtime, and there's no way to craft a "project default".

Oddly, the only sane choice is STRING, mostly because ORDINAL is brittle; the order of your enums is forever locked in (data surgery notwithstanding), so I can't understand why the default is ORDINAL - I have never heard of any project using the it.

Related