1

I'm trying to get some entities inheritance to behave as I understand the docs. Here is what I understand from hibernate javadoc, hibernate user guide, or this question:

Having this inheritance schema:

     Parent         <= @Inheritance(strategy = InheritanceType.JOINED)
    /      \
ChildA      ChildB  <= @Polymorphism(type = PolymorphismType.EXPLICIT)
  • When I query Parent, I get only data from the supertype.
  • If I want to get data from a Child along with the supertype's one, I have to query the subtype directly.

I find very interresting the fact that I can store most commonly used fields into the supertype, so I can make big queries on the Parent without joins and smaller queries on the Childs.

The problem is that Hibernate keeps fetching Child with join when I query Parent:

select
    childb0_.id as id1_2_0_,
    childb0_1_.stringAttribute as stringAt2_2_0_,
    childb0_.intAttribute as intAttri1_1_0_
from
    ChildB childb0_
inner join
    Parent childb0_1_
        on childb0_.id=childb0_1_.id
where
    childb0_.id=?

And thus, either using Jpql or Criteria:

List<?> results = jpaApi.em()
    .createQuery("FROM Parent")
    .getResultList();

or

List<?> results = jpaApi.em()
    .unwrap(Session.class)
    .createCriteria(Parent.class)
    .list();

We made a dummy project to illustrate and reproduce the problem.

I realize that this question might seem like a replicate of this one, but why would PolymorphismType.EXPLICIT exist if hibernate does an IMPLICIT join either way ?

4

0 回答 0