package relations.m2mattr;

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

/**
 * ============================================================================
 * 关系情形 ③：多对多【带属性】—— Project
 * ============================================================================
 *
 * 【关系 / Relationship】
 * EN — The other side of the same M:N, again through the Assignment entity.
 *      Note the difference from example 02: there is NO @JoinTable and NO @ManyToMany here.
 * 中 — 同一个多对多的另一端，同样通过 Assignment 实体连接。
 *      注意与情形 ② 的区别：这里没有 @JoinTable，也没有 @ManyToMany。
 *
 * 【为什么两端不能都写 orphanRemoval / Why only one side may own the lifecycle】
 * EN — Assignment is a child of the link, not of Project. If both Employee and Project used
 *      orphanRemoval = true, Hibernate would try to delete the same row from two directions
 *      and may throw "deleted object would be re-saved by cascade".
 * 中 — Assignment 是关联的子记录，不属于 Project。如果 Employee 和 Project 两端都写
 *      orphanRemoval = true，Hibernate 会从两个方向删同一行，可能抛
 *      “deleted object would be re-saved by cascade”。
 */
@Entity
public class Project {

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

    private String name;

    @OneToMany(mappedBy = "project")
    private List<Assignment> assignments = new ArrayList<>();

    public Project() { }
    public Project(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<Assignment> getAssignments() { return assignments; }
}
