Mysql 向自引用父级/子级添加一对多关系
使用Spring和Hibernate,我可以在自引用类和另一个类中的父/子类之间实现一对多关系。也就是说,这是自参考类: DB: 型号:Mysql 向自引用父级/子级添加一对多关系,mysql,spring,hibernate,Mysql,Spring,Hibernate,使用Spring和Hibernate,我可以在自引用类和另一个类中的父/子类之间实现一对多关系。也就是说,这是自参考类: DB: 型号: @Entity @Table(name="employee") public class Employee { @Id @Column(name="employee_id") @GeneratedValue private Long employeeId; @Column(name="name") private Str
@Entity
@Table(name="employee")
public class Employee {
@Id
@Column(name="employee_id")
@GeneratedValue
private Long employeeId;
@Column(name="name")
private String name;
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="manager_id")
private Employee manager;
@OneToMany(mappedBy="manager")
private Set<Employee> employee = new HashSet<Employee>();
这是我试图实现的概述,但我想知道这是否可行,如果可行,我将如何在DB关系中设置它,并通过hibernate将关系保存到DB。感谢您的回复。这就是我在Java类中实现的关系,但我不确定的部分是将其转换为数据库,特别是通过Manager id(实际上是外键)实现的Courses和Manager之间的关系。创建表
courses
(courseid
BIGINT(10)非空自动增量,manager\u id
BIGINT(20)空默认空,讲师id
BIGINT(20)空默认空,
@OneToMany(mappedBy="manager")
private List<Course> managedCourses = new ArrayList<Course>();
@OneToMany(mappedBy="lecturer")
private List<Course> lectuedCourses = new ArrayList<Course>();
@OneToMany(mappedBy="manager")
private List<Course> course = new ArrayList<Course>();
@OneToMany(mappedBy="lecturer")
private List<Course> courses = new ArrayList<Course>();
@Entity
@Table(name = "courses")
@Component
public class Course implements Serializable
@ManyToOne
@JoinColumn(name="employee_id", insertable=false, updatable=false)
private Employee employee;
@ManyToOne
@JoinColumn(name="manager_id", insertable=false, updatable=false)
private Employee manager;
@OneToMany(mappedBy="manager")
private List<Course> managedCourses = new ArrayList<Course>();
@OneToMany(mappedBy="lecturer")
private List<Course> lectuedCourses = new ArrayList<Course>();
@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;