问题描述
我有以下实体:
public class Category {
private Integer id;
@OneToMany(mappedBy =parent)
private List< Topic>主题;
}
public class Topic {
private Integer id;
@OneToMany(mappedBy =parent)
私人列表<文章>帖子;
@ManyToOne
@JoinColumn(name =id)
私人类别父;
}
public class Post {
private Integer id;
@ManyToOne
@JoinColumn(name =id)
private主题parent;
/ *发布字段* /
}
我想要获取所有类别使用JPQL查询加入主题并加入帖子。我写了如下所示的查询:
SELECT c FROM Category c JOIN FETCH c.topics t JOIN FETCH t.posts p WHERE。 ..
但是我收到错误
<$
$ / pre
org.hibernate.loader.MultipleBagFetchException:不能同时获取多个行李
我找到了关于这个错误的文章,但是这些文章只描述了在一个实体中有两个要加入的集合的情况。我的问题有点不同,我不知道如何解决它。
可以在一个查询中完成吗?
对不起,我的英语不好,但我通常用其他语言说话
href =http://vladmihalcea.com/hibernate-facts-multi-level-fetching/ =noreferrer> Child-Parent fetch策略并重新组合结果中的实体树。 选择p
FROM发布p
JOIN FETCH p.topic t
JOIN FETCH t.category c
WHERE ...
I have below entities:
public class Category {
private Integer id;
@OneToMany(mappedBy = "parent")
private List<Topic> topics;
}
public class Topic {
private Integer id;
@OneToMany(mappedBy = "parent")
private List<Posts> posts;
@ManyToOne
@JoinColumn(name = "id")
private Category parent;
}
public class Post {
private Integer id;
@ManyToOne
@JoinColumn(name = "id")
private Topic parent;
/* Post fields */
}
and I want fetch all categories with joined topics and joined posts using JPQL query. I was wrote query like below:
SELECT c FROM Category c JOIN FETCH c.topics t JOIN FETCH t.posts p WHERE ...
But I got the error
org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags
I found articles about this error, but these articles only describe situation where in one entity are two collections to join. My problem is a little different and I don't know How to solve it.
It is possible to do in one query?
Sorry for my bad english, but I usually speak in other language
You can use a Child-Parent fetch strategy and recombine the entity tree from the result.
SELECT p
FROM Post p
JOIN FETCH p.topic t
JOIN FETCH t.category c
WHERE ...
这篇关于在一个JPQL查询中多次联接提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!