gpt4 book ai didi

java - JPA : How to define @NamedEntityGraph for 3 levels?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:57:47 25 4
gpt4 key购买 nike

我有 3 个实体。分支,主题,主题。Branch 有主题列表,Subject 有主题列表。还subjectList 和 topicList 都是惰性的。我想获取所有分支在单个查询中包括其主题和主题。

1.

@Entity
public class Branch implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;

@OneToMany(mappedBy = "branch")
private List<Subject> subjectList;
//Getters and Setters
}

2.

@Entity
public class Subject implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

private String name;

@ManyToOne()
private Branch branch;

@OneToMany(mappedBy = "subject")
private List<Topic> topicList;
//Getters and Setters
}

3.

@Entity
public class Topic implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;

@ManyToOne()
private Subject subject;
//Getters and Setters
}

我尝试了下面的方法,但没有用。

@NamedEntityGraph(name="branch_subject", 
attributeNodes = {
@NamedAttributeNode(value="name"),
@NamedAttributeNode(value="subjectList", subgraph = "subjectListGraph")
},
subgraphs = {
@NamedSubgraph(name="subjectListGraph",
attributeNodes = {
@NamedAttributeNode(value="name"),
@NamedAttributeNode(value = "topicList", subgraph = "topicListGraph")
}
),
@NamedSubgraph(name="topicListGraph",
attributeNodes = {
@NamedAttributeNode("name")
}
)
}
)

下面的代码也用于从数据库,我正在使用 JPQL,如下所示

    EntityGraph branchEntityGraph = entityManager
.getEntityGraph("branch_subject");

Branch branch = entityManager
.createQuery("SELECT b from Branch b WHERE b.id=:ID",
Branch.class)
.setHint("javax.persistence.loadgraph", branchEntityGraph)
.setParameter("ID", branch1.getId()).getResultList().get(0);

这给出了以下异常

org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags

最佳答案

Hibernate 不允许您获取多个包,因为它最终会获取笛卡尔积。

M → N → P 一对多或多对多关系

对于多嵌套层次结构,您可以对多个集合使用JOIN FETCH,只要您的列表被映射为Set

M → N 和M → P 一对多或多对多关系

对于兄弟集合,如 M → N 和 M → P,不要切换到使用 Set 而不是 List

使用 Set 而不是 List 来避免 MultipleBagFetchException这是一个非常糟糕的主意,因为你仍然会得到一个 Cartesian Product ,这会导致性能问题,因为您要获取 M x N x P 记录。

在这种情况下,更好的方法是使用第一个查询获取一个集合,并对其余集合使用额外的查询:

List<Post> _posts = entityManager.createQuery("""
select distinct p
from Post p
left join fetch p.comments
where p.id between :minId and :maxId
""", Post.class)
.setParameter("minId", 1L)
.setParameter("maxId", 50L)
.setHint(QueryHints.PASS_DISTINCT_THROUGH, false)
.getResultList();

_posts = entityManager.createQuery("""
select distinct p
from Post p
left join fetch p.tags t
where p in :posts
""", Post.class)
.setParameter("posts", _posts)
.setHint(QueryHints.PASS_DISTINCT_THROUGH, false)
.getResultList();

此策略允许您通过获取 M x (N + P) 记录来避免 M x N x P 结果集。

从子端获取到父端

如果在获取子集合时必须使用 INNER JOIN,那么您可以简单地[从最里面的子集合获取到根][3],然后重新组装结构。这样效率更高,因为查询是这样的:

select t 
from Topic t
join t.subject s
join s.branch b

关于java - JPA : How to define @NamedEntityGraph for 3 levels?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30479748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com