The possiblity for a new inhertance type ( Incremental )

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

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

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.

1 Like

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

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.

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.

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

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.

@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 {
}