0
votes

In section 10.1.2. Table per subclass it talks about creating inheritance via a one-to-one mapping across several tables.

https://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/#inheritance-tablepersubclass

When all the examples on the internet talk about "Table per subclass" they are actually talking about 10.1.3. Table per subclass: using a discriminator.

My question is how does the 10.1.2. Table per subclass know which class to instantiate without a discriminator column.

If the answer is that hibernate does 3 (or whatever) extra queries to find the location of the data then why would you use this method when the discriminator method guarantees just 2 queries.

1

1 Answers

2
votes

If you look at the query, it will have some kind of switch statement like the one below

select
    account0_.id as id1_9_,
    account0_.balance as balance2_9_,
    account0_1_.checkLimitAmount as checkLim1_10_,
    account0_2_.atmLimit as atmLimit1_11_,
    case 
        when account0_1_.id is not null then 1 
        when account0_2_.id is not null then 2 
        when account0_.id is not null then 0 
    end as clazz_ 
from
    INHERITANCE_JTND_ACCOUNT account0_ 
left outer join
    INHERITANCE_JTND_CHECKING_ACCOUNT account0_1_ 
        on account0_.id=account0_1_.id 
left outer join
    INHERITANCE_JTND_SAVINGS_ACCOUNT account0_2_ 
        on account0_.id=account0_2_.id

so it does only one query. Hibernate then uses the clazz_ column to figure out what to instantiate. The query above is from HSQLDB, and might be different for other DB engines.

It's usually a good idea to print the SQL statements that JPA/Hibernate generates (at least on your local environment) as sometimes you'll be surprised of the DML that it generates.