使用联接继承时联接 child table table [谓词]

Join child table when using joined inheritance table [Predicates]

我有parent:child继承模型:

@Entity
@Table(name = "PARENT")
@Inheritance(strategy = InheritanceType.JOINED)
public class Parent {
  ...
  id
}

@Entity
@Table(name = "CHILD")
public class Child extends Parent {
   ... 
   @OneToMany(mappedBy = "id", orphanRemoval = true, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
   private Set<Foo> foos = new HashSet<>();
}

如何使用 Predicates 加入这两个 table?前提是根是 Root<Parent>。这已经由 JPA 默认完成,但我需要在这两者之间进行 join object 访问以进一步加入 Foo 列表。

CriteriaQuery<Parent> q = cb.createQuery(Parent.class);
Root<Parent> r = q.from(Parent.class);

Join<Parent, Child> cJoin = r.join("???", JoinType.LEFT);  <-- ERROR
cJoin.on(cb.equal(r.get("id"), cJoin.get("id")));

Join<Child, Foo> fJoin = cJoin.join("foos", JoinType.LEFT);
fJoin.on(cb.equal(r.get("id"), cJoin.get("id")));

不知道childtable名字怎么表达,看代码示例中的"???"。 这会产生 Unable to locate Attribute with the the given name [???]。我试过写 child, Child 无济于事。

或者有其他方法可以达到同样的目的吗?

JPA CriteriaQuery Join - how to join on a subelement? 解决方案不是一个选项。

我最终使用了子查询:

CriteriaQuery<Parent> q = cb.createQuery(Parent.class);
Root<Parent> r = q.from(Parent.class);

Subquery<Long> childSubQuery = q.subquery(Long.class);
Root<Child> childRoot = childSubQuery.from(Child.class);
Join<Child, Foo> fJoin = childRoot.join("foos", JoinType.LEFT);
fJoin.on(cb.equal(childRoot.get("id"), fJoin.get("id")));
childSubQuery.select(childRoot.get("id"));
childSubQuery.where(cb.equal(fJoin.get("condition"), conditionValue));

q.where(cb.equal(r.get("id"), childSubQuery));

注意:显然,如果有机会,请使用上述 link 中建议的构图。