Hibernate / JPA: duplicates in the children's collection

My data model consists of schools and students. Students have FK to the School in which they belong. I do not understand why the collection contains duplicates, i.e. Joe, Joe, Mary, Mary, Tom, Tom, etc. The SQL query generated by Hibernate is correct and does not return duplicates. I could implement a hack to filter out duplicates, but I'm not ready to live with a broken window yet;) I tried to insert the corresponding code snippets below. Any help is much appreciated!

// 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;
}
+5
source share
3 answers

, FetchType.EAGER , , Hibernate, .

SET , , , , .

Eran Medan :

+13

, :

  • , (, , , , , , , ).
  • , equals/hashCode ( , ).

, ? , , .

+1

, , . , , , .

0

All Articles