package relations.many2many;

import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * ============================================================================
 * 关系情形 ②：多对多 M:N（无属性）—— Student
 * ============================================================================
 *
 * 【关系 / Relationship】
 * EN — One student takes many courses; one course has many students. The relationship itself
 *      carries NO extra data, so a plain @ManyToMany is enough. Only ONE side declares
 *      @JoinTable (the owning side); the other side uses mappedBy.
 * 中 — 一个学生选多门课，一门课有多个学生；关系本身没有额外数据，所以用普通 @ManyToMany。
 *      只有一端写 @JoinTable（拥有端），另一端用 mappedBy。
 *
 * 【生成表 / Generated tables】
 *   student(id, name)
 *   course(id, title)
 *   student_course(student_id, course_id)     ← 只有两列
 *
 * 【考试判断 / Exam cue】
 * EN — True only when nothing besides "who is linked to whom" must be stored.
 *      For example the real exam's Author <-> Book mapping uses exactly this pattern.
 * 中 — 仅当“谁和谁关联”之外没有别的信息要存时才这么写。真题里 Author ↔ Book 就是这种。
 */
@Entity
public class Student {

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

    private String name;

    @ManyToMany
    @JoinTable(
            name = "student_course",
            joinColumns = @JoinColumn(name = "student_id"),
            inverseJoinColumns = @JoinColumn(name = "course_id"))
    private List<Course> courses = new ArrayList<>();

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

    public Long getId() { return id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public List<Course> getCourses() { return courses; }
}
