I am trying to design an entity model with inheritance that also supports maintaining draft and publish states of the entities. I am using JPA annotations with Hibernate implementation (version 6.5). I have the following entity model
- abstract Message
- abstract TextRoot
- Text
- TextPublish
- TextDraft
- Text
- abstract WhatsappRoot
- Whatsapp
- WhatsappPublish
- WhatsappDraft
- Whatsapp
- abstract TextRoot
Here is what I want to achieve
- all of the classes here are entities and I want to support polymorphic queries (i.e. fetch all
Messages
or fetch all Texts) - I want to create tables for all of the entities I have highlighted in bold to represent a single table for each sub hierarchy
- The entities highlighted in bold will have “publish” and “draft” entities and the entities highlighted in bold will have tables that contain both publish and draft entities
I tried using a mix of two JPA inheritance strategies, SINGLE_TABLE
and TABLE_PER_CLASS
.
I annotated Message
with TABLE_PER_CLASS
and annotate Text
and Whatsapp
with SINGLE_TABLE
. I do understand as per JPA spec it isn’t recommended. However, I believe I need both strategies because I need an inheritance strategy to be applied to the root class so that I can tell Hibernate I want to support polymorphic queries at that level but create a table per “sub hierarchy” by using SINGLE_TABLE
.
In the end it didn’t work as expected - Hibernate seems to ignore the SINGLE_TABLE
inheritance strategy and generate DDLs using only the TABLE_PER_CLASS
inheritance strategy applied on the Message
entity class.
Can anyone suggest how I try to achieve a mixed inheritance strategy for my use case?