1

Parent table and class

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Table(name = "vehicle")
public class Vehicle extends Entity {
    // Attributes
}

create table vehicle
(
    id varchar2(50) not null,
    // Attributes
)tablespace :TABLESPACE_DATA ;

Child 1 table and class

@Entity
@Table(name = "car")
public class Car extends Vehicle {
    // Attributes
}

create table car
(
    // Attributes
) inherits (vehicle) tablespace :TABLESPACE_DATA ;

Child 2 table and class

@Entity
@Table(name = "bus")
public class Bus extends Vehicle {
    // Attributes
}

create table bus
(
    // Attributes
) inherits (vehicle) tablespace :TABLESPACE_DATA ;

If I add a new Car, then there will be two records with same id, one in a parent table, other in a child table.

Then with a Hibernate generated query (check below) it will return with two rows, which cause an exception.

select
    // ...
from
    (
        select
            id,
            //
            0 as clazz_
        from
            VEHICLE
        union all
        select
            id,
            //
            1 as clazz_
        from
            car
        union all
        select
            id,
            //
            2 as clazz_
        from
            bus
    ) vehicle0_
where
    vehicle0_.id = '5105f2d9-9c69-44c1-9368-0b3013a3a058'

The Hibernate version 5.4.28.

What could be the issue?

tmsblgh
  • 517
  • 5
  • 21

2 Answers2

0

That should not happen. When doing an insert, Hibernate ORM should only insert that entity in the right table. So if you create a new Car, only an insert in the car table should occur.

Hibernate ORM expects this mapping to work with three separates table. The reason it does an union is because if you run a query like "from Vehicle", it needs to check all the tables.

Check the Hibernate ORM documentation for more details about the mapping.

Davide D'Alto
  • 7,421
  • 2
  • 16
  • 30
  • The copied query returns with two rows, both have the same id. Then org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader#extractEntityResult method throwing an exception, like "More than one row with the given identifier was found". So it can be return with more than one row if the Hibernate code is prepared to handle that case. – tmsblgh Apr 23 '21 at 09:59
  • Actually, what I said is completely wrong. Hibernate ORM should only insert an entry in the right table. How are you doing the insert of a Car? – Davide D'Alto Apr 23 '21 at 10:13
  • I need to correct my self, the Car is only one place. The inherited table is similar like a view, so it just "showing" the row. I mean The row is visible from Vehicle and Car table as well, but if I remove it from the Vehicle it will disappear from the other as well. The problem I think is with the Hibernate, why it is using UNION ALL for the Parent and for the Child tables? – tmsblgh Apr 23 '21 at 12:55
  • That's because if you do an HQL query like "from Vehicle", it needs to look for entities in all the tables. Hibernate ORM expects 3 separates tables for this mapping, but it seems that's not the case in your database. – Davide D'Alto Apr 23 '21 at 13:03
  • I was expecting inheritance to only inherit the schema, not the data as well – Davide D'Alto Apr 23 '21 at 13:12
  • 1
    The problem was the missing "abstract" keyword. After I added it to the Vehicle then the Hibernate skipped the parent from the query. – tmsblgh Apr 23 '21 at 13:43
0

The problem was the missing "abstract" keyword. After I added it to the Vehicle then the Hibernate skipped the parent from the query.

Solution found here: Hibernate TABLE_PER_CLASS with @MappedSuperclass won't create UNION query

tmsblgh
  • 517
  • 5
  • 21