[英]Hibernate @OneToMany mapped by 2 different columns
I have an object called Lecture and it has 2 fields which are both of type Teacher 我有一个名为Lecture的对象,它有2个字段,均为教师类型
class Lecture{
private Teacher teacher;
private Teacher subsTeacher;
}
Lets assume a Teacher may teach many lectures. 假设一位老师可以教很多课。 So there is a one to many relation between Teacher -> Lecture 所以老师与老师之间是一对多的关系
Now in Teacher class I have: 现在在老师班上,我有:
@OneToMany(mappedBy = "teacher")
@Cascade(value = {org.hibernate.annotations.CascadeType.DETACH,
org.hibernate.annotations.CascadeType.LOCK})
public Set<Lecture> getLectures() {
return lectures;
}
However, this will not return me the Lectures the teacher is subsTeacher to. 但是,这不会将我的老师退回给老师的讲座退还给我。 But I need them as well. 但是我也需要它们。
How can I achieve this? 我该如何实现?
Table structure: 表结构:
Table: Lecture 表:讲座
Columns: id, teacher_id, substeacher_id 列:id,teacher_id,substeacher_id
Table Teacher: 表老师:
Columns: id, name 列:ID,名称
For this you need to filter lectures according to teacher/subteacher and manually set to the lectures list. 为此,您需要根据老师/子老师过滤课程并手动设置到课程列表。 Otherwise you need to do separate mapping. 否则,您需要进行单独的映射。 if you need to save the lectures with the teacher you should add separate mapping and use accordingly. 如果您需要与老师保存课程,则应添加单独的映射并相应地使用。
@OneToMany(mappedBy = "subsTeacher", cascade = {CascadeType.ALL})
private Set<Lecture> subLectures;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.