Java enum backed by table/entity

Hi

In a project I java enums that are somewhat complex with multiple fields defining various business logic. I’m looking for a way to have these enums backed by a table/entity.

Optimally the enum could be an entity itself, but as far at I can tell the JPA spec does not allow for that. So question is if there is a way to somehow define an entity and map instances of one-to-one the the enum constants. Or is there a different way?

What I try to achieve is:

  • Maintain full DDL with annotations (including foreign keys pointing to the enum table).
  • Be able to create database views that join the entity table for more human readable representation (the system will save millions of records, so its important that the column that holds the enum reference can be as small as possible, probably a single byte).

I expect to write some application init code that ensures that the backing table is synced with the enum (if new constants were added).

Sorry I my question is a little vague, but I’m have trouble wording it, please let me know if you need clarifications.

Best regards Jens

So I actually managed to get this working by annotating the enum as an Entity. I had to create an Interceptor to work around the fact that enums does not and can not have a no-args constructor.

I will see if I can clean up the example and push it to github.

Best regards Jens