gpt4 book ai didi

hibernate/JPA : duplicates in child collection

转载 作者:行者123 更新时间:2023-12-04 10:05:54 25 4
gpt4 key购买 nike

我的数据模型由学校和学生组成。学生对他们所属的学校有 FK。
我不明白为什么该集合包含重复项,即 Joe、Joe、Mary、Mary、Tom、Tom 等
Hibernate 生成的 SQL 查询是正确的,不会返回重复项。
我可以实现一个 hack 来过滤掉重复项,但我还没有准备好忍受 splinter 的 window ;)
我试图粘贴下面的相关代码段。非常感谢任何帮助!

// SchoolJpa

@OneToMany (
mappedBy = "school",
targetEntity = StudentJpa.class,
fetch = FetchType.LAZY,
cascade = CascadeType.ALL
)
@Override
public List<Student> getStudentsInternal() {
return super.getStudentsInternal();
}

// SchoolImpl

private List<Student> students = new ArrayList<Student>();

public List<Student> getStudents() {
return Collections.unmodifiableList(students);
}

public List<Student> getStudentsInternal() {
return students;
}

public void setStudentsInternal(List<Students> students) {
this.students = students;
}

最佳答案

我的猜测是您在 School 中有一个 FetchType.EAGER 或其他映射,这将导致 Hibernate 发出外部联接查询,这将导致列表中出现重复项。

将类型切换为 SET 是可行的,因为集合自然会根据相等性进行重复数据删除,因此外连接查询返回的重复项会丢失。

Eran Medan 提供的更详尽的解释可用 here :

关于 hibernate/JPA : duplicates in child collection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3247657/

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