向自我引用父/子添加一对多关系 [英] Adding a one to many relationship to a self reference parent/child

查看:101
本文介绍了向自我引用父/子添加一对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Spring和Hibernate,我可以在自引用类和另一个类中实现父/子之间的一对多关系。也就是说,这是自引用类:

DB:

  CREATE TABLE`employee`(
`employee_id` BIGINT(10)NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50)NULL DEFAULT NULL,
`manager_id` BIGINT(20)NULL DEFAULT NULL,
PRIMARY KEY(`employee_id`),
CONSTRAINT`FK_MANAGER` FOREIGN KEY(`manager_id`)REFERENCES`employee`
$ b(`employee_id`))

型号:

  @Entity 
@Table(name =employee)
public class Employee {

@Id
@Column(name =employee_id)
@GeneratedValue
private Long employeeId;

@Column(name =name)
私有字符串名称;

@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name =manager_id)
私人员工经理;

@OneToMany(mappedBy =manager)
私人设置< Employee> employee = new HashSet< Employee>();

现在我想为父/子(经理/员工)创建一对多关系)和另一个类如下:

  @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 Courseizable实现Serializable

@ManyToOne
@JoinColumn(name =employee_id,insertable = false,updatable = false)
私人员工雇员;

@ManyToOne
@JoinColumn(name =manager_id,insertable = false,updatable = false)
私人员工经理;

这是我想要实现的概述,但我想知道这是否可行如果是的话我该如何设置它在DB关系中,并且能够通过hibernate保存与db的关系。

解决方案

  @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 Courseizable实现Serializable

@ManyToOne
@JoinColumn(name =lecturer_id,可插入= false,可更新= false)
私人员工讲师;

@ManyToOne
@JoinColumn(name =manager_id,insertable = false,updatable = false)
私人员工经理;


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 class:

DB:

  CREATE TABLE `employee` (
  `employee_id` BIGINT(10) NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(50) NULL DEFAULT NULL,
  `manager_id` BIGINT(20) NULL DEFAULT NULL,
  PRIMARY KEY (`employee_id`),
  CONSTRAINT `FK_MANAGER` FOREIGN KEY (`manager_id`) REFERENCES `employee`    

  (`employee_id`))

Model:

  @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>();

Now I want to create a one to many relationship for both of the parent/child (manager/employee) and another class like this:

  @OneToMany(mappedBy="manager")
  private List<Course> course = new ArrayList<Course>();

  @OneToMany(mappedBy="lecturer")
  private List<Course> courses = new ArrayList<Course>();

So both of manager and employee will have an association with one or many courses. The course class:

  @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;

That's the overview of what I'm trying to implement but I would like to know if this is possible and if so how would I set it up in a DB relationship and and be able to save the relationship to db via hibernate.

解决方案

@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;

这篇关于向自我引用父/子添加一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆