给出以下伪实体模型:
@Entity
@Table(name = "parent")
public class Parent {
@Id
private Long id;
@OneToMany(mappedBy="parent")
private List<Child> children;
}
@Entity
@Table(name = "child")
public class Child {
@Id
private Long id;
@ManyToOne
@JoinColumn(name="parent_id")
private Parent parent;
@ElementCollection
@CollectionTable(name = "child_hobby", joinColumns = @JoinColumn(name = "id"))
private List<Hobby> hobbies;
}
public enum Hobby {
DANCE, SING, RUN
}
一个人如何在一次提取中提取parent
以及它们的children
及其相应爱好的列表?
到目前为止,我尝试的是处理@OneToMany
关系时通常要做的事情:
QBean<Parent> parentProjection = Projections.fields(Parent.class, QParent.parent.id);
QBean<Child> childProjection = Projections.fields(Child.class, QChild.child.id);
EnumPath<Hobby> hobbies = Expressions.enumPath(Hobby.class, "hobbies");
Map<Parent, Group> result = from(QParent.parent)
.innerJoin(QParent.parent.children, QChild.child)
.innerJoin(QChild.child.hobbies, hobbies);
.transform(GroupBy.groupBy(parentProjection).as(GroupBy.list(childProjection), GroupBy.list(hobbies);
result.forEach((parent, groups) -> {
parent.setChildren(groups.getList(childProjection));
// How to get the hobbies of the parent's children?
});
上面的片段运行正常,但是我无法从投影组中检索child.hobbies
。在QueryDSL文档的Result Aggregation部分中,我找不到任何可以帮助这种情况的信息。