Adding a one to many relationship to a self reference parent/child

后端 未结 1 836
鱼传尺愫
鱼传尺愫 2021-01-26 08:21

Using Spring and Hibernate, can I implement a one to many relationship between the parent/child in a self reference class and another class. That is, this is the self reference

1条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-26 08:22

    @OneToMany(mappedBy="manager") 
    private List managedCourses = new ArrayList();
    
    @OneToMany(mappedBy="lecturer")
    private List lectuedCourses = new ArrayList();
    

    ...

    @Entity
    @Table(name = "courses")
    @Component
    public class Course implements Serializable
    
    @ManyToOne
    @JoinColumn(name="lecturer_id", insertable=false, updatable=false)
    private Employee lecturer;
    
    @ManyToOne
    @JoinColumn(name="manager_id", insertable=false, updatable=false)
    private Employee manager;
    

    0 讨论(0)
提交回复
热议问题