package relations.tworelations;

import jakarta.persistence.*;

/**
 * ============================================================================
 * 关系情形 ⑥：同一对实体的两条关联 —— Doctor
 * ============================================================================
 *
 * 【关系 / Relationship】
 * EN — A doctor may HEAD one department and also be a MEMBER of one department. Both
 *      relationships point at Department, so each must have its own foreign key column:
 *      head_dept_id and member_dept_id.
 * 中 — 一位医生可能“主管”一个科室，同时也是另一个科室的“成员”。两段关系都指向 Department，
 *      所以必须各用一列外键：head_dept_id 与 member_dept_id。
 *
 * 【生成表 / Generated table】
 *   doctor(id, name, head_dept_id, member_dept_id)
 *
 * 【常见错误 / Common mistake】
 * EN — Using the same column name (or leaving @JoinColumn out) makes Hibernate map only one
 *      of the two relationships; the other silently disappears.
 * 中 — 两段关系用同一个列名（或不写 @JoinColumn）时，Hibernate 只会映射其中一段，
 *      另一段会悄悄消失。
 */
@Entity
public class Doctor {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    @ManyToOne
    @JoinColumn(name = "head_dept_id")
    private Department headed;

    @ManyToOne
    @JoinColumn(name = "member_dept_id")
    private Department joined;

    public Doctor() { }
    public Doctor(String name) { this.name = name; }

    public Long getId() { return id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public Department getHeaded() { return headed; }
    public void setHeaded(Department headed) { this.headed = headed; }
    public Department getJoined() { return joined; }
    public void setJoined(Department joined) { this.joined = joined; }
}
