【发布时间】:2019-01-31 07:08:44
【问题描述】:
我的两个实体之间有@ManyToMany 关系。当我尝试将父母与他们的孩子一起加载时,父母会重复响应有效载荷。我想用EntityGraphs解决我的问题。
这是带有注释的父级。
@Entity
@Table(name="Parent_table")
@NamedEntityGraph(
name = "Parent.children",
attributeNodes = @NamedAttributeNode("children"))
public class Parent implements Serializable{
//some extra code
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(
name="join table", joinColumns=@JoinColumn(name="key"),inverseJoinColumns=@JoinColumn(name="key"))
private List<Child> children;
这是存储库中父级的代码。
@EntityGraph(value = "Parent.children", type = EntityGraphType.LOAD)
public List<Parent> findAll(Predicate predicate);
我的两个实体之间存在多对多关系。但我想得到类似的结果
parent1 {
child1,
child2
}
但我为每个组合都得到了父母..
Parent1 {
child1,
child2
}
Parent1 {
child1,
child2
}
我得到了 parent2。
但我想要的是只获得一次 parent1,而不是重复。 使用实体图时。
【问题讨论】:
-
父母在响应负载中重复。“响应负载”是什么意思?如果您谈论的是 JSON 表示,那么
@EntityGraph不会帮助您。 -
响应负载是指我得到的 JSON 输出。我多次得到每个父母的组合(等于孩子的数量)
标签: spring hibernate spring-boot spring-data-jpa entitygraph