package relations.many2many;

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

/**
 * ============================================================================
 * 关系情形 ②：多对多 M:N（无属性）—— Course（反向端）
 * ============================================================================
 *
 * 【关系 / Relationship】
 * EN — The inverse side. It must NOT declare @JoinTable again: doing so creates a SECOND
 *      join table and the two sides stop seeing each other's data.
 * 中 — 反向端。这里不能再写 @JoinTable，否则会生成第二张中间表，两边的数据互相看不见。
 *
 * 【对照 / Contrast】
 * EN — If the sign-up also stored, say, the enrolment date or a grade, this pattern would
 *      become WRONG and you must introduce an intermediate entity (see 03).
 * 中 — 如果选课还要记录“选课时间”或“成绩”，这种写法就错了，必须引入中间实体（见 03）。
 */
@Entity
public class Course {

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

    private String title;

    @ManyToMany(mappedBy = "courses")
    private List<Student> students = new ArrayList<>();

    public Course() { }
    public Course(String title) { this.title = title; }

    public Long getId() { return id; }
    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }
    public List<Student> getStudents() { return students; }
}
