package relations.one2one;

import jakarta.persistence.*;

/**
 * ============================================================================
 * 关系情形 ④：一对一 1:1 —— Employee（拥有端）
 * ============================================================================
 *
 * 【关系 / Relationship】
 * EN — One employee has exactly one desk. The owning side declares @OneToOne with
 *      @JoinColumn(unique = true); the unique constraint is what keeps it 1:1 instead of 1:N.
 * 中 — 一个员工恰好一个工位。拥有端写 @OneToOne + @JoinColumn(unique = true)；
 *      unique 约束才是“一对一”的关键，漏了它就退化成一对多。
 *
 * 【生成表 / Generated tables】
 *   employee(id, name, desk_id UNIQUE)
 *   desk(id, floor, zone)
 *
 * 【考试判断 / Exam cue】
 * EN — "each X has exactly one Y" → 1:1. If the sentence says "may have", it is 1:0..1 and
 *      the same annotation applies, only nullable = true.
 * 中 — “每个 X 恰好一个 Y” → 1:1。若题面说“可能有”，那是 1:0..1，注解不变、只是 nullable = true。
 */
@Entity
public class Employee {

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

    private String name;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "desk_id", unique = true)
    private Desk desk;

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

    public Long getId() { return id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public Desk getDesk() { return desk; }
    public void setDesk(Desk desk) { this.desk = desk; }
}
