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 ?

stamm
  • 129
  • 1
  • 10
  • I found some other thread that relates to this question : [this one](https://stackoverflow.com/questions/17277500/hibernate-polymorphism-explicit-annotation-doesnt-work) seems to agree with my opinion on how it should work, and [this one](https://stackoverflow.com/questions/16541129/avoiding-outer-joins-across-tables-when-using-joined-inheritance-with-spring-dat?rq=1) says that EclipseLink v2.5.0 does the trick, i'll give it a try. – stamm Nov 30 '17 at 10:12
  • It's also possible to use a class mapped to the parent entity and that has no inheritance information. Like [this](https://github.com/stammw/hibernate-polymorphism-test/blob/28c32efade76609130093d55928ede5beed139b6/app/models/ParentShallow.java#L13) and [there](https://github.com/stammw/hibernate-polymorphism-test/blob/28c32efade76609130093d55928ede5beed139b6/test/models/TestDao.java#L50). This is not very clean but works and could be improved using a special hierarchy for `Parent`. – stamm Nov 30 '17 at 10:31

0 Answers0