Using Spring and Hibernate, can I implement a one-to-many relationship between the parent / child in the self-promotion class and the other class. That is, this is a self-defined 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 the parent / child (manager / employee) and another class as follows:
@OneToMany(mappedBy="manager") private List<Course> course = new ArrayList<Course>(); @OneToMany(mappedBy="lecturer") private List<Course> courses = new ArrayList<Course>();
Thus, both the manager and the employee will have a connection with one or more courses. 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;
This is an 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 against the database and keep in touch with db via hibernate.
source share