# The possiblity for a new inhertance type ( Incremental )

**URL:** <https://discourse.hibernate.org/t/the-possiblity-for-a-new-inhertance-type-incremental/5581>\
**Category:** Hibernate ORM\
**Created:** [August 1, 2021, 3:37pm UTC](https://discourse.hibernate.org/t/the-possiblity-for-a-new-inhertance-type-incremental/5581 "2021-08-01T15:37:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![OmarAbdullwahhab](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/omarabdullwahhab/32/1603_2.png) [@OmarAbdullwahhab](https://discourse.hibernate.org/u/OmarAbdullwahhab)\
**Post date:** [August 1, 2021, 3:37pm UTC](https://discourse.hibernate.org/t/the-possiblity-for-a-new-inhertance-type-incremental/5581/1 "2021-08-01T15:37:26Z")

</div>

Hello everyone,  
While I was working in a modular application in Java,  
I have noticed a new and a good pattern to  
add in the JPA api, currently in JPA we have three types  
of inheritance types

1. TABLE\_PER\_CLASS
2. SINGLE\_TABLE
3. JOINED.
4. MappedSuperclass

The new pattern is completely the reverse of the MappedSupperclass  
In MappedSuperclass we map all fields to the Subclass’s table

In the suggested Strategy we shall map all the fields to the Super class table  
without discriminator column, for example

```auto
class BusinessParty {
}
class Customer extends BusinessParty {
}
class Guest extends BusinessParty {
}

```

any field that is added to the customer or guest class is added directly to  
the parent BusinessParty table  
I can also query all objects in the database with any class in the in the hierarchy  
if  
Select a from Account a  
Select g from Guest g  
is executed it should give the same result.  
I know that is a limitation in OOP in class casting, but that can be guarded and implemented in ORM Framework

thank you

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [August 2, 2021, 8:03am UTC](https://discourse.hibernate.org/t/the-possiblity-for-a-new-inhertance-type-incremental/5581/2 "2021-08-02T08:03:55Z")

</div>

Well, this is what the `SINGLE_TABLE` inheritance strategy does, but it needs a discriminator, otherwise there is no way to understand what kind of class to use for a particular row. What objects should Hibernate create in your opinion when querying `select b from BusinessParty` which roughly translates to SQL `select * from business_party_table`? Without the discriminator, there simply is no way to model the class hierarchy.

---

<div class="post-metadata">

**Author:** ![OmarAbdullwahhab](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/omarabdullwahhab/32/1603_2.png) [@OmarAbdullwahhab](https://discourse.hibernate.org/u/OmarAbdullwahhab)\
**Post date:** [August 2, 2021, 10:52am UTC](https://discourse.hibernate.org/t/the-possiblity-for-a-new-inhertance-type-incremental/5581/3 "2021-08-02T10:52:39Z")

</div>

Thank you @beikov ,  
well, the SINGLE\_TABLE is good and fine for the scenario if guests and customers are treated differently, but let’s say that in the table

```auto
id, DTYPE, Guest_PROP, Cust_PROP, BusinessParty_PROP,
1 Guest, XXXX , NULL, GUEST_NAME,
2 Cust, NULL, XX, CUST_NAME,

```

if I issued select \* from business\_party\_table  
it should return all the rows,  
with the help of reflection I can determine which fields I can map,  
for example JPQL  
select g from Guest g,  
should return **both records** without _Cust\_PROP_ column,  
the same for  
select c from Customer c  
should return **both records** without _Guest\_PROP_ column.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [August 2, 2021, 11:41am UTC](https://discourse.hibernate.org/t/the-possiblity-for-a-new-inhertance-type-incremental/5581/4 "2021-08-02T11:41:50Z")

</div>

That doesn’t make much sense IMO because that would mean you could have two distinct objects with the same primary key in the same table, yet the objects have a different Java type. Such a mapping would not be bidirectional and would lead to problems. You can still map that though if you like. Just use the mapped-superclass approach and for each class-specific property, you add the mapping to the respective class. Both entities would map to the same table.

---

<div class="post-metadata">

**Author:** ![OmarAbdullwahhab](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/omarabdullwahhab/32/1603_2.png) [@OmarAbdullwahhab](https://discourse.hibernate.org/u/OmarAbdullwahhab)\
**Post date:** [August 2, 2021, 4:44pm UTC](https://discourse.hibernate.org/t/the-possiblity-for-a-new-inhertance-type-incremental/5581/5 "2021-08-02T16:44:23Z")

</div>

Thank you @beikov  
but please have a look at a very small sample for declaration of my idea  
here is the github link  
[https://github.com/OmarAbdullwahhab/extreme-opensource](https://github.com/OmarAbdullwahhab/extreme-opensource)  
in that sample I have declared only DDL create table, and DML select statement , please have a look  
also if you can guide me how can I ask or contribute to hibernate orm to add this case  
in this example I have created three entity classes  
UserCaseInfo  
Employee  
SystemUser

I do not need to solve this by the current mappings rather I want to implement or add this to hibernate ORM  
Thank you again

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [August 3, 2021, 6:26am UTC](https://discourse.hibernate.org/t/the-possiblity-for-a-new-inhertance-type-incremental/5581/6 "2021-08-03T06:26:53Z")

</div>

The problem isn’t SQL or DDL generation, but how do you want to determine which object to create for a JDBC result row? If you want to use some nullable field to determine the class, then you can already do that with Hibernate. You will just have to encode the selection in a discriminator formula i.e.

```auto
@DiscriminatorFormula("case when guest_prop is not null then 1 when cust_prop is not null then 2 else null end")
class BusinessParty {
}
@DiscriminatorValue("2")
class Customer extends BusinessParty {
}
@DiscriminatorValue("1")
class Guest extends BusinessParty {
}

```
