# How can I mix inheritance strategies (SINGLE\_TABLE and TABLE\_PER\_CLASS) with Hibernate 6.4/JPA?

**URL:** <https://discourse.hibernate.org/t/how-can-i-mix-inheritance-strategies-single-table-and-table-per-class-with-hibernate-6-4-jpa/9887>\
**Category:** Hibernate ORM\
**Created:** [July 4, 2024, 7:33am UTC](https://discourse.hibernate.org/t/how-can-i-mix-inheritance-strategies-single-table-and-table-per-class-with-hibernate-6-4-jpa/9887 "2024-07-04T07:33:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Surya\_Sanjiv](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/surya_sanjiv/32/2486_2.png) [@Surya\_Sanjiv](https://discourse.hibernate.org/u/Surya_Sanjiv)\
**Post date:** [July 4, 2024, 7:33am UTC](https://discourse.hibernate.org/t/how-can-i-mix-inheritance-strategies-single-table-and-table-per-class-with-hibernate-6-4-jpa/9887/1 "2024-07-04T07:33:16Z")

</div>

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

  - abstract WhatsappRoot
    - **Whatsapp**
      - WhatsappPublish
      - WhatsappDraft

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?**

---

<div class="post-metadata">

**Author:** ![mbladel](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mbladel/32/2128_2.png) [@mbladel](https://discourse.hibernate.org/u/mbladel)\
**Post date:** [July 4, 2024, 7:39am UTC](https://discourse.hibernate.org/t/how-can-i-mix-inheritance-strategies-single-table-and-table-per-class-with-hibernate-6-4-jpa/9887/2 "2024-07-04T07:39:09Z")

</div>

You **cannot** use different inheritance strategies in the same inheritance hierarchy. The `@Inheritance` annotation, used to specify which `InheritanceStrategy` to use, must be placed on the root entity type and will be used for all the child entities.

Looking at your class list, it would fit to use `@MappedSuperclasses`, instead of single-table inherited entities, but not that this will mean not having a corresponding table on the database-side. If you still want to maintain separate tables even for the abstract root classes, you’re going to have to choose either the single-table or the table-per-class strategy for the whole hierarchy.

---

<div class="post-metadata">

**Author:** ![Surya\_Sanjiv](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/surya_sanjiv/32/2486_2.png) [@Surya\_Sanjiv](https://discourse.hibernate.org/u/Surya_Sanjiv)\
**Post date:** [July 4, 2024, 10:54am UTC](https://discourse.hibernate.org/t/how-can-i-mix-inheritance-strategies-single-table-and-table-per-class-with-hibernate-6-4-jpa/9887/3 "2024-07-04T10:54:59Z")

</div>

Firstly, thank you for your reply! I appreciate it.  
So, if i use @MappedSuperclass, wouldn’t it be rather impossible to use polymorphic queries on those classes as they wont be annotated with @Entity?

---

<div class="post-metadata">

**Author:** ![mbladel](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mbladel/32/2128_2.png) [@mbladel](https://discourse.hibernate.org/u/mbladel)\
**Post date:** [July 4, 2024, 10:59am UTC](https://discourse.hibernate.org/t/how-can-i-mix-inheritance-strategies-single-table-and-table-per-class-with-hibernate-6-4-jpa/9887/4 "2024-07-04T10:59:06Z")

</div>

No, it won’t. Hibernate supports polymorphic HQL queries across `@MappedSuperclass` types. You won’t be able to fetch superclass-typed entities through normal `Session` methods, though, e.g. `session.find( id, MappedSuperclassType.class )` will not work.
