Steve Chambers Steve Chambers - 1 month ago 14
Java Question

How to join to a JOINED subclass in Hibernate / JPA 2.0 in a CriteriaQuery?

I need to join from a superclass attribute to its subclass entity (using JOINED inheritance). Tried something like this:

root.join(RootClass_.superClassAttribute).as(SubClass.class)


...and need to continue this by joining from the subclass to other entities.
But the
as
is returning a
CastFunction
so not sure how this is possible?

(Note: Am using JPA 2.0, not 2.1 so can't use
treat
.)

Answer

In the end I got it working by constructing the query the other way round - so instead of attempting to join to a subclass it was simply using the parent metamodel class:

inverseRoot.join(InverseRootClass_.subClassAttribute).join(ParentClass.attribute)...

This worked for my use case but expect there may still be situations where the question needs a better answer - e.g. where the required set of joins includes both children and parents.