將兩個OneToMany
關聯添加到我的實體類似乎不起作用。如果我刪除其中一個,它工作正常。JPA實體是否可以有多個OneToMany關聯?
@Entity
@Table(name = "school")
public class School {
private List<Teacher> teachers;
private List<Student> students;
@OneToMany(cascade=CascadeType.ALL, mappedBy = "school", fetch = FetchType.EAGER)
public List<Teacher> getTeachers()
return this.teachers;
}
public void setTeachers(List<Teacher> teachers) {
this.teachers = teachers;
}
@OneToMany(cascade=CascadeType.ALL, mappedBy = "school", fetch = FetchType.EAGER)
public List<Student> getStudents()
return this.students;
}
public void setStudents(List<Student> teachers) {
this.students = students;
}
}
然後在Teacher
和Student
我有正確的ManyToOne
註釋
@Entity
@Table(name = "teacher")
public class Teacher {
private School school;
@ManyToOne
@JoinColumn(name = "school_id")
public School getSchool() {
return this.school;
}
public void setSchool(School school) {
this.school = school;
}
}
@Entity
@Table(name = "student")
public class Student {
private School school;
@ManyToOne
@JoinColumn(name = "school_id")
public School getSchool() {
return this.school;
}
public void setSchool(School school) {
this.school = school;
}
}
我也有id
領域與正確的註釋(@Id
,@GeneratedValue
)
所以對我來說好像我在同一班級中不能有多於一個@OneToMany
。它是否正確?
不,它根本不正確。你可以擁有儘可能多的你想要的。你是什麼意思「它不工作」? – Kayaman
可能是兩個關係具有相同名稱的問題? –
當我檢索用戶時,沒有任何數據從數據庫返回。沒有堆棧跟蹤,甚至沒有警告。我可以看到hibernate映射了所有類 – steven35